UITextView - 禁用垂直滚动
如何禁用 UITextView 中的垂直滚动?我希望它基本上只是水平滚动。
How can I disable vertical scrolling in my UITextView? I want it to basically just scroll horizontally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在某些情况下,当试图限制不需要的 UITextView 滚动时,我发现向 UITextView 委托添加类似以下内容很有帮助(这是一个 UIScrollView 委托方法,但当然,UITextView 继承自 UIScrollView)。这可能对你有用。
那么scrollEnabled属性呢?将scrollEnabled属性设置为NO会阻止用户滚动(双向),但有时系统会向UITextView发送
setContentOffset:animated:
消息。 scrollEnabled 属性适用于垂直和水平滚动。鉴于您的问题,您可能希望保持scrollEnabled不变。In some circumstances, when trying to clamp down on unwanted UITextView scrolling I have found it helpful to add something like the following to the UITextView delegate (this is a UIScrollView delegate method but, of course, UITextView inherits from UIScrollView). This might work for you.
What about the scrollEnabled property? Setting the scrollEnabled property to NO stops the user from scrolling (in both directions), but there are occasions where the system sends
setContentOffset:animated:
messages to a UITextView. The scrollEnabled property applies to both vertical and horizontal scrolling. Given your question, you might want to leave scrollEnabled as is.您可以从 Xcode 更改它 -
You can change it from Xcode -
禁用 Swift 4 垂直滚动的解决方案:
Solution for disabling vertical scrolling for Swift 4:
如果您有自定义 textView 子类,则可以覆盖 -gestureRecognizerShouldBegin 以禁用滚动。
If you have your custom textView subclass, you can override -gestureRecognizerShouldBegin to disable the scroll.
如果您不想垂直滚动,为什么不直接使用 UITextField 呢?
why not just use a UITextField if you dont want vertical scrolling?
只需将
contentSize
设置为视图的高度即可。你将使用这个:
Just set the
contentSize
to the height of the view.You'll use this:
将 UITextView 放在 UIScrollView 中。
将 UITextView.frame 设置为完整文本适合一行的大小,并将 ScrollView 的 contenSize 设置为 UITextView.frame 的大小。
干杯
内茨
place your UITextView in a UIScrollView.
Set your UITextView.frame to a Size the complete Text fits in a Line and set the contenSize of the ScrollView to the size of your UITextView.frame.
Cheers
nettz