ios 如果用户点击屏幕键盘,我如何关闭键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5711434/
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
How can I dismiss the keyboard if a user taps off the on-screen keyboard?
提问by Sheehan Alam
I want to be able to dismiss the iPhone keyboard when the user taps anywhere outside of the keyboard. How can I go about doing this? I know I need to dismiss the responder, but need to know how to implement it when a user taps out of the keyboard space.
当用户点击键盘外的任何地方时,我希望能够关闭 iPhone 键盘。我该怎么做呢?我知道我需要解雇响应者,但需要知道当用户点击键盘空间时如何实现它。
回答by visakh7
You'll need to add an UITapGestureRecogniser
and assign it to the view, and then call resign first responder on the textfield on it's selector.
您需要添加一个UITapGestureRecogniser
并将其分配给视图,然后在其选择器上的文本字段上调用 resign first responder。
The code:
编码:
In viewDidLoad
在 viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
在 dismissKeyboard:
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
(Where aTextField is the textfield that is responsible for the keyboard)
(其中 aTextField 是负责键盘的文本字段)
OPTION 2
选项 2
If you can't afford to add a gestureRecognizer then you can try this
如果你负担不起添加一个gestureRecognizer,那么你可以试试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
回答by Taneem Tee
The simplest solution I have used is this:
我使用过的最简单的解决方案是:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
The endEditing command can be used on any view that contains your textfield as a subview. The other advantage of this method is that you don't need to know which textfield triggered the keyboard. So even if you have a multiple textfields, just add this line to the superview.
endEditing 命令可用于包含您的文本字段作为子视图的任何视图。这种方法的另一个优点是您不需要知道哪个文本字段触发了键盘。因此,即使您有多个文本字段,只需将此行添加到超级视图中即可。
Based on the Apple documentation, I think this method exists specifically to solve this problem.
根据Apple文档,我认为这种方法专门用于解决此问题。
回答by Matt Rees
Add a tapGesture Recognizer but make sure cancelsTouchesInView = NO
添加一个 tapGesture Recognizer 但确保 cancelsTouchesInView = NO
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
回答by Suresh Kansujiya
You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.
您需要添加一个 UITapGestureRecogniser 并将其分配给视图,然后在其选择器的文本字段上调用 resign first responder。
The code:
编码:
In viewDidLoad
在视图中加载
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
在解雇键盘中:
-(void)dismissKeyboard {
[self.view endEditing:true];
}
回答by Kimdv
This is a Swift 4 solution:
这是一个 Swift 4 解决方案:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
self.view.addGestureRecognizer(tap)
And the dismissKeyboard
和解雇键盘
@objc func dismissKeyboard() {
self.view.endEditing(true)
}
回答by Shreesh
You need to add a transparent UIVIew as a subview below the keyboard and handle touches there, to dismiss the keyboard. Below code is for your reference.
您需要添加一个透明的 UIVIew 作为键盘下方的子视图并处理那里的触摸,以关闭键盘。以下代码供您参考。
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(overlayTouched:)];
gesture.delegate = self;
[(UITapGestureRecognizer *)gesture setNumberOfTouchesRequired:1];
UIView* trans = [[UIView alloc] initWithFrame:[[delegate view] bounds]];
[trans setOpaque:NO];
[trans setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[trans setAlpha:0.3];
[trans setUserInteractionEnabled:YES];
trans.multipleTouchEnabled = YES;
[trans addGestureRecognizer:gesture];
[trans setBackgroundColor:[UIColor blackColor]];
[trans setTag:BLACK_SCREEN_VIEW];
回答by Anu Mittal
If you don't want to add a gesture recognizer you can also use the touchesBegin method
如果您不想添加手势识别器,也可以使用 touchesBegin 方法
Code in Swift 4
Swift 4 中的代码
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) }
回答by Zindokar
Other way to do is simple:
其他方法很简单:
Makes your UIView as UIControl in custom class in the interface builder
, then you can attach an IBAction
method in Touch up inside event
of your UIView : UIControl
, then you put [yourTextField resignFirstResponder]
inside the IBAction method
, like this:
使你的UIView as UIControl in custom class in the interface builder
,那么你可以将一个IBAction
在方法Touch up inside event
你的UIView : UIControl
,然后你把[yourTextField resignFirstResponder]
里面的IBAction method
,就像这样:
- (IBAction) hideKeyboard: (id) sender
{
// If you have more than one textfield to dismiss, of course only can be active 1, but here you can't know who is it, because sender will be the UIView : UIControl
[alias resignFirstResponder];
[password resignFirstResponder];
}
Then, you have other option and it's to put in your textfield the return key of the keyboard as Done
(It can be any of those you want, but Done it's good for this, because return means to do an action with the form) in the interface builder, so you can press Done
and hide the keyboard, but in that case you have to attach the previous IBAction method to the Did end on exit
event.
然后,您还有其他选择,它是the return key of the keyboard as Done
在界面构建器中放入您的文本字段(它可以是您想要的任何一个,但完成它对此有好处,因为返回意味着对表单执行操作),因此您可以按Done
并隐藏键盘,但在这种情况下,您必须将先前的 IBAction 方法附加到Did end on exit
事件。
And in this way the keyboard will hide touching outside
or touching Done
from the keyboard.
通过这种方式,键盘将隐藏touching outside
或触摸Done
键盘。
If you want to improve the code, if only will hide the keyboard touching Done
from the keyboard the method should be:
如果你想改进代码,如果只是将键盘Done
从键盘上隐藏起来,方法应该是:
// Attach all textFields here on Did end on exit event, will not work if touch outside the keyboard
- (IBAction) hideKeyboard: (id) sender
{
[sender resignFirstResponder];
}
回答by Adam Hockemeyer
If you are on a UITableViewController
or have a UITableView
that the user will be interacting with, in your ViewDidLoad you can simply do:
如果您在UITableViewController
或有一个UITableView
用户将与之交互,则在您的 ViewDidLoad 中,您可以简单地执行以下操作:
tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;
Note: this is Xamarin.iOS syntax.
注意:这是 Xamarin.iOS 语法。