如果用户点击屏幕键盘,我该如何关闭键盘?

时间:2011-04-19 03:58:21

标签: iphone objective-c cocoa-touch ios uitextfield

当用户点击键盘外的任何地方时,我希望能够关闭iPhone键盘。我该怎么做呢?我知道我需要解雇响应者,但需要知道当用户从键盘空间中弹出时如何实现它。

9 个答案:

答案 0 :(得分:80)

您需要添加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];
    }
}

答案 1 :(得分:42)

我使用的最简单的解决方案是:

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

      [[self view] endEditing:TRUE];

}

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

根据Apple文档,我认为此方法专门用于解决此问题。

答案 2 :(得分:16)

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

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

答案 3 :(得分:2)

您需要添加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];
}

答案 4 :(得分: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];

答案 5 :(得分:1)

这是一个Swift 4解决方案:

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

 self.view.addGestureRecognizer(tap)

和dismissKeyboard

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

答案 6 :(得分:0)

其他方法很简单:

制作UIView as UIControl in custom class in the interface builder,然后您可以在IBAction的{​​{1}}附加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并隐藏键盘,但在这种情况下,您必须将以前的IBAction方法附加到Done事件。

通过这种方式键盘将隐藏Did end on exit或触摸键盘上的touching outside

如果你想改进代码,如果只是隐藏键盘触摸键盘Done,方法应该是:

Done

答案 7 :(得分:0)

如果您使用UITableViewController或者UITableView用户将与之互动,则可以在ViewDidLoad中执行以下操作:

tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;

注意:这是Xamarin.iOS语法。

答案 8 :(得分:0)

如果您不想添加手势识别器,也可以使用touchesBegin方法

Swift 4中的代码

  

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) }