我希望能够在用户点击键盘外的任何位置时关闭iPhone键盘。我该怎么做呢?我知道我需要解雇响应者,但需要知道当用户从键盘空间中弹出时如何实现它。
你需要添加一个UITapGestureRecogniser
并将其分配给视图,然后在它的选择器上的文本字段上调用resign first responder。
代码:
在viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
在dismissKeyboard:
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
(其中aTextField是负责键盘的文本字段)
方案2
如果您无法承担添加gestureRecognizer的费用,那么您可以试试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
我用过的最简单的解决方案是:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:TRUE];
}
endEditing命令可用于包含文本字段作为子视图的任何视图。此方法的另一个优点是您不需要知道哪个文本字段触发了键盘。因此,即使您有多个文本字段,也只需将此行添加到superview即可。
基于Apple文档,我认为这种方法专门用于解决这个问题。
添加tapGesture识别器但请确保cancelsTouchesInView = NO
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
您需要添加一个UITapGestureRecogniser并将其分配给视图,然后在其选择器的文本字段上调用resign first responder。
代码:
在viewDidLoad中
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
在dismissKeyboard中:
-(void)dismissKeyboard {
[self.view endEditing:true];
}
您需要在键盘下方添加一个透明的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];
这是一个Swift 4解决方案:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
self.view.addGestureRecognizer(tap)
和dismissKeyboard
@objc func dismissKeyboard() {
self.view.endEditing(true)
}
其他方法很简单:
使你的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];
}
然后,你有其他的选择,它可以放入你的文本字段the return key of the keyboard as Done
(它可以是你想要的任何一个,但是它对此有好处,因为return意味着在表单中执行操作),所以你可以按Done
并隐藏键盘,但在这种情况下,你必须将以前的IBAction方法附加到Did end on exit
事件。
通过这种方式键盘将隐藏touching outside
或从键盘触摸Done
。
如果你想改进代码,如果只是隐藏键盘触摸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];
}
如果您使用的是UITableViewController
或者有一个用户将要与之交互的UITableView
,您可以在ViewDidLoad中执行以下操作:
tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;
注意:这是Xamarin.iOS语法。
如果您不想添加手势识别器,也可以使用touchesBegin方法
Swift中的代码4
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) }