关闭 UIScrollView 中的键盘
好吧,我在 UIScrollView
中有几个 UITextFields
和 UITextViews
,我想将键盘设置为在 UIScrollView
出现时消失。 >scrollview 被触摸或滚动(当然,当您触摸文本字段/视图内部时除外)。
我当前的尝试是将 UIScrollView 替换为子类,并将其设置为调用 touchesBegan 方法内的removeKeyboard 函数(在主视图控制器内定义)。但是,这只会删除正常触摸时的键盘,而不是在简单滚动视图时删除键盘。那么,在 UIScrollView
中删除键盘的最佳方法是什么?
预先感谢您的帮助。
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).
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
?
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这是在 iOS 7.0 及更高版本中实现此目的的最简洁方法。
或者
用 Swift 表示:
或者
Here is the cleanest way to achieve this in iOS 7.0 and above.
Or
In Swift:
Or
有点晚了,但如果其他人正在寻找这个问题的答案,这就是我解决它的方法:
1)创建一个带有目标回调方法的点击手势识别器,以在所有字段上使用 resignFirstResponder 关闭键盘。
2) 将点击手势添加到滚动视图。
这是一个例子:
Bit late but if anyone else is searching an answer to this problem, this is how I have gone about solving it:
1) Create a tap gesture recognizer with a target callback method to dismiss your keyboard using resignFirstResponder on all your fields.
2) Add the tap gesture to the scrollview.
Here's an example:
虽然本质是一样的,但我更喜欢更少的代码。
在属性检查器中设置滚动视图滚动时键盘消失:
然后滚动视图时消失键盘被点击:
scrollView.endEditing(true)
。如果您使用的是 Objective-C,[self.scrollView endEditing: YES];
Although the essence is the same, I prefer less code.
Setting the keyboard to disappear when the scrollView is scrolled in Attributes inspector:
Then disappear keyboard when scrollView is tapped:
scrollView.endEditing(true)
. If you are using Objective-C,[self.scrollView endEditing: YES];
在 Swift 中:
有点晚了,但如果其他人正在寻找这个问题的答案,这就是我解决它的方法:
1)创建一个带有目标回调方法的点击手势识别器来关闭键盘在所有字段上使用 resignFirstResponder 。
2) 将点击手势添加到滚动视图。
这是一个例子:
In Swift:
Bit late but if anyone else is searching an answer to this problem, this is how I have gone about solving it:
1) Create a tap gesture recognizer with a target callback method to dismiss your keyboard using resignFirstResponder on all your fields.
2) Add the tap gesture to the scrollview.
Here's an example:
查看 UIScrollView 的 keyboardDismissMode 属性。
迅捷版
Look at keyboardDismissMode property of UIScrollView.
Swift Version
创建一个扩展类,用于在触摸任何地方的滚动视图/视图时隐藏键盘
并在 viewDidLoad 中调用此方法,如下所示
Create a extension class for hiding keyboard when touches scrollview/view anywhere
And call this method in viewDidLoad like
试试这个
Try This
有点晚了,但如果其他人正在使用 Swift 3 寻找这个问题的答案:
A bit late but if anyone else is searching an answer to this problem with Swift 3:
当我将手势添加到 UIScrollView 的子类时,我遇到了视图树中各种手势相互干扰的问题,例如能够单击子视图、滚动视图以及键盘在所有情况下都会消失。我想出了这个解决方案,它可以从 UIScrollView 的超类或 UIViewController 进行设置。
DismissKeyboardTapGesture
类使用 ARC,可处理视图下的任何文本字段,并且不会接管来自子视图(如按钮)的任何点击。还利用iOS7的滚动效果来消除键盘。从 UIScrollView 超类设置:
或从 UIViewController 设置:
这是该类:
When I added the gesture to a subclass of
UIScrollView
, I was having problems with the various gestures in my view tree interfering with each other, such as being able to click on subviews, scroll the view, and have the keyboard dismiss in all cases. I came up with this solution, which can be setup from a superclass ofUIScrollView
or from aUIViewController
.The
DismissKeyboardTapGesture
class uses ARC, works with any text fields under the view, and doesn't take over any clicks from subviews like buttons. Also takes advantage of iOS7 scrolling effect to dismiss keyboard.Setting up from UISScrollView superclass:
or from UIViewController:
Here is the class:
尝试这个滚动视图委托方法 -
将 IB 中的委托链接到滚动视图,然后复制此代码(根据您的需要进行修改)。
这应该有效。您也可以尝试其他委托方法,例如
Try this scroll view delegate method -
link delegate in IB to scroll view and then cop this code (modify as per your need).
This should work. You can try other delegate methods too like