ios 在 UIScrollView 中关闭键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5143873/
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
Dismissing the keyboard in a UIScrollView
提问by Nicholas1024
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).
好吧,我有几个UITextFields
和UITextViews
在 a 中UIScrollView
,我想将键盘设置为在scrollview
触摸或滚动时消失(当然,当您在文本字段/视图内触摸时除外)。
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 touchesBeganmethod. 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
?
我目前尝试这样做是用UIScrollView
子类替换,并将其设置为在touchesBegan方法中调用 removeKeyboard 函数(在主视图控制器中定义)。但是,这只会在正常触摸时移除键盘,而不是在简单滚动视图时。那么,移除键盘内部的最佳方法是UIScrollView
什么?
Thanks in advance for your help.
在此先感谢您的帮助。
回答by Pei
Here is the cleanest way to achieve this in iOS 7.0 and above.
这是在 iOS 7.0 及更高版本中实现此目标的最简洁方法。
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
Or
或者
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
In Swift:
在斯威夫特:
scrollView.keyboardDismissMode = .onDrag
Or
或者
scrollView.keyboardDismissMode = .interactive
回答by Zhang
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.
1) 创建一个带有目标回调方法的点击手势识别器,以在所有字段上使用 resignFirstResponder 关闭键盘。
2) Add the tap gesture to the scrollview.
2) 将点击手势添加到滚动视图。
Here's an example:
下面是一个例子:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = NO;
[pageScrollView addGestureRecognizer:tapGesture];
[tapGesture release];
...
// method to hide keyboard when user taps on a scrollview
-(void)hideKeyboard
{
[myTextFieldInScrollView resignFirstResponder];
}
回答by fujianjin6471
Although the essence is the same, I prefer less code.
虽然本质是一样的,但我更喜欢更少的代码。
Setting the keyboard to disappear when the scrollView is scrolled in Attributes inspector:
在属性检查器中滚动 scrollView 时将键盘设置为消失:
Then disappear keyboard when scrollView is tapped:
然后在点击 scrollView 时消失键盘:
- Drag a Tap Gesture Recognizer onto your scrollView
- Only one line in the action ——
scrollView.endEditing(true)
. If you are using Objective-C,[self.scrollView endEditing: YES];
- 将点击手势识别器拖到您的滚动视图上
- 动作中只有一行——
scrollView.endEditing(true)
。如果您使用的是 Objective-C,[self.scrollView endEditing: YES];
回答by King-Wizard
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.
1) 创建一个带有目标回调方法的点击手势识别器,以在所有字段上使用 resignFirstResponder 关闭键盘。
2) Add the tap gesture to the scrollview.
2) 将点击手势添加到滚动视图。
Here's an example:
下面是一个例子:
import UIKit
class ViewController: UIViewController {
@IBOutlet var t1: UITextField!
@IBOutlet var t2: UITextField!
@IBOutlet var t3: UITextField!
@IBOutlet var t4: UITextField!
@IBOutlet var srcScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard")
// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = false
srcScrollView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
t1.resignFirstResponder()
t2.resignFirstResponder()
t3.resignFirstResponder()
t4.resignFirstResponder()
}
}
回答by Naresh Reddy M
Look at keyboardDismissModeproperty of UIScrollView.
查看UIScrollView 的keyboardDismissMode属性。
// will hide keyboard when your text field is about to go beyond the keyboard.
vwScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
// will hide keyboard instantly once the scroll view started scrolling by user.
vwScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissOnDrag;
// If you need to hide keyboard on tap of scroll view,consider adding a tap gesture or sub class and override touchesbegan: method.
Swift Version
迅捷版
vwScrollView.keyboardDismissMode = .interactive
vwScrollView.keyboardDismissMode = .onDrag
回答by chandra1234
Create a extension class for hiding keyboard when touches scrollview/view anywhere
创建一个扩展类,用于在任何地方触摸滚动视图/视图时隐藏键盘
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
}
}
And call this method in viewDidLoad like
并在 viewDidLoad 中调用此方法,例如
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
回答by Saad Ur Rehman
Try This
尝试这个
[self.selectedViewController.view endEditing:YES]
;
[self.selectedViewController.view endEditing:YES]
;
回答by Jorge Casariego
A bit late but if anyone else is searching an answer to this problem with Swift 3:
有点晚了,但如果其他人正在使用Swift 3搜索此问题的答案:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
view.endEditing(true)
}
回答by Skotch
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 of UIScrollView
or from a UIViewController
.
当我将手势添加到 的子类时UIScrollView
,我遇到了视图树中各种手势相互干扰的问题,例如能够单击子视图、滚动视图以及在所有情况下都关闭键盘。我想出了这个解决方案,它可以UIScrollView
从UIViewController
.
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.
该DismissKeyboardTapGesture
班采用弧形,这样的观点在任何文本字段工作,并且不会像纽扣子视图接管任何点击。还利用 iOS7 滚动效果来关闭键盘。
Setting up from UISScrollView superclass:
从 UISScrollView 超类设置:
_dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self];
or from UIViewController:
或来自 UIViewController:
_dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self.view];
Here is the class:
这是课程:
@interface DismissKeyboardTapGesture : NSObject <UIGestureRecognizerDelegate>
@end
@implementation DismissKeyboardTapGesture
- (id)initWithView:(UIView *)view
{
self = [super init];
if (self) {
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTap.cancelsTouchesInView = NO;
singleTap.delegate = self;
[view addGestureRecognizer:singleTap];
if ([view respondsToSelector:@selector(setKeyboardDismissMode:)]) {
// Bonus effect to dismiss keyboard by scrolling
((UIScrollView *)view).keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
}
}
return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// Don't stop any existing gestures in our view from working
if (otherGestureRecognizer.view == gestureRecognizer.view) {
return YES;
}
return NO;
}
- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
// Close keyboard for any text edit views that are children of the main view
[gestureRecognizer.view endEditing:YES];
}
@end
回答by user1140780
Try this scroll view delegate method -
试试这个滚动视图委托方法 -
link delegate in IB to scroll view and then cop this code (modify as per your need).
在 IB 中链接委托以滚动视图,然后复制此代码(根据您的需要进行修改)。
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//sample code
[challengeABallotComponent.voterNameTextField resignFirstResponder];
[challengeABallotComponent.ballotNumberTextField resignFirstResponder];
[locationInformation.pollingLocation resignFirstResponder];
}
This should work. You can try other delegate methods too like
这应该有效。您也可以尝试其他委托方法,例如
-(void)scrollViewDidScroll: (UIScrollView *)scrollView
{
//do your stuff
}