问题描述
好吧,我在 UIScrollView中有几个
,我想在触摸或滚动 UITextFields
和 UITextViews
scrollview
时将键盘设置为消失(除非在文本字段/视图中触及时,当然)。
Alright, I have a couple of UITextFields
and UITextViews
inside a UIScrollView
, and I'd like to set the keyboard to disappear whenever the scrollview
is touched or scrolled (except when you touch down inside the text field/view, of course).
我目前尝试这样做是用子类替换 UIScrollView
,并将其设置为在 touchesBegan 方法中调用removeKeyboard函数(在主视图控制器中定义)。但是,这只会移除键盘以进行正常触摸,而不是仅在滚动视图时。那么,在 UIScrollView
中删除键盘的最佳方法是什么?
My current attempt at doing this is replacing the UIScrollView
with a subclass, and setting it to call a removeKeyboard function (defined inside the main view controller) inside the touchesBegan method. However, this only removes the keyboard for a normal touch, not when the view is simply scrolled. So, what's the best way to remove the keyboard inside a UIScrollView
?
提前感谢您的帮助。
推荐答案
这是在iOS 7.0及更高版本中实现此目的的最简洁方法。
Here is the cleanest way to achieve this in iOS 7.0 and above.
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
或
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
Swift:
scrollView.keyboardDismissMode = .onDrag
或
scrollView.keyboardDismissMode = .interactive
这篇关于在UIScrollView中关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!