2011-04-19 103 views
38

我希望能够在用户轻敲键盘外的任何地方时关闭iPhone键盘。我怎么能这样做呢?我知道我需要解雇响应者,但是当用户敲出键盘空间时需要知道如何实现它。如果用户点击屏幕键盘,我该如何解除键盘锁定?

+0

你可能会觉得这很有用:https://github.com/michaeltyson/TPKeyboardAvoiding – 2011-04-19 04:01:48

回答

73

你需要添加一个UITapGestureRecogniser,并将其分配给视图,然后调用辞职在它选择的文本字段第一个响应。

的代码:

viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(dismissKeyboard)]; 

[self.view addGestureRecognizer:tap]; 

dismissKeyboard:

-(void)dismissKeyboard { 
     [aTextField resignFirstResponder]; 
} 

(其中aTextField是负责用于键盘的文本字段)

OPTION 2

如果你不能添加gestureRecognizer那么你可以试试这个

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch * touch = [touches anyObject]; 
    if(touch.phase == UITouchPhaseBegan) { 
     [aTextField resignFirstResponder]; 
    } 
} 
+0

非常优雅的解决方案w /手势识别器! – 2011-04-20 16:45:56

+4

这很棒,但是,手势操作正在吃掉视图上的所有按钮点击事件。有任何想法吗? – kmehta 2011-04-29 21:09:59

+0

解决键盘在ios中的好解决方案。 – 2013-01-31 10:57:16

1

您需要在键盘下面添加一个透明的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]; 
39

我用最简单的办法是这样的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

     [[self view] endEditing:TRUE]; 

} 

的endEditing命令可用于上包含您的文本字段作为子视图的任何视图。这种方法的另一个优点是你不需要知道哪个文本字段触发了键盘。因此,即使您有多个文本框,只需将此行添加到超级视图即可。

基于Apple的文档,我认为这种方法专门用于解决这个问题。

+0

“UIScrollView”呢? – testing 2014-12-01 13:47:50

+0

这是最好的解决方案没有DOUBT! – StackUnderflow 2017-03-31 12:01:11

15

添加tapGesture识别器,但要确保cancelsTouchesInView = NO

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)]; 
tapGesture.cancelsTouchesInView = NO; 
[self.view addGestureRecognizer:tapGesture]; 
[tapGesture release]; 
+0

关于tapGesture.cancelsTouchesInView = NO; 非常感谢! – Miros 2014-03-17 14:48:22

0

做其他的方法很简单:

让您UIView as UIControl in custom class in the interface builder,那么您可以在您的UIView : UIControlTouch up inside event附加一个IBAction方法,然后你把[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(它可以是您希望的任何一种,但完成它对此很有用,因为返回意味着对表单执行操作),所以您可以按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]; 
} 
0

如果你是一个UITableViewController或有UITableView,该用户将被与你的ViewDidLoad交互,你可以简单地做:

tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag; 

注意:这是Xamarin.iOS语法。

2

您需要添加一个UITapGestureRecogniser并将其分配给该视图,然后在其选择器上的文本字段上调用resigned first responder。

的代码:

在viewDidLoad中

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(dismissKeyboard)]; 

[self.view addGestureRecognizer:tap]; 

在dismissKeyboard:

-(void)dismissKeyboard { 
     [self.view endEditing:true]; 
} 
0

这是一个夫特4溶液:

let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard)) 

self.view.addGestureRecognizer(tap) 

而dismissKeyboard

@objc func dismissKeyboard() { 
    self.view.endEditing(true) 
}