iOS -- UITextView 禁用滚动但启用触摸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4413248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS -- UITextView disable scrolling but enable touches
提问by William Jockusch
Is there any way to do this?
有没有办法做到这一点?
What I really want is a UITextField where I can control the location of the cursor. But as far as I can tell, that is impossible. So I am looking at using a UITextView. This does allow one to control the cursor location. But scrolling is getting in the way -- my text is scrolling here and there, and I don't want that.
我真正想要的是一个 UITextField,我可以在其中控制光标的位置。但据我所知,这是不可能的。所以我正在考虑使用 UITextView。这确实允许控制光标位置。但是滚动妨碍了——我的文本在这里和那里滚动,我不希望那样。
采纳答案by William Jockusch
If I set scrollEnabled to NO and have an empty implementation of scrollRectToVisible:animated:, it appears to disable scrolling.
如果我设置scrollEnabled为NO,并有一个空的实现scrollRectToVisible的:动画:,这似乎禁用滚动。
回答by James Bedford
UITextView is a subclass of UIScrollView, so you can prevent the view from scrolling by setting the property scrollEnabledto NO.
的UITextView是UIScrollView的子类,这样就可以防止视图通过设置该属性滚动scrollEnabled到NO。
回答by Mark McDonald
Just set the UITextView's content size to be less than or equal to its bounds, this will prevent scrolling and still accept touch events.
只需将 UITextView 的内容大小设置为小于或等于其边界,这将阻止滚动并仍然接受触摸事件。
回答by Victor Camargo
Swift 3, Swift 4:
斯威夫特 3,斯威夫特 4:
let textView = UITextView()
textView.isScrollEnabled = false