如果用户点击屏幕键盘,我怎样才能closures键盘?

我希望能够在用户点击键盘外的任何地方时closuresiPhone键盘。 我怎么能这样做? 我知道我需要closures响应者,但是当用户敲击键盘空间时需要知道如何实现它。

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

代码:

viewDidLoad

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

dismissKeyboard:

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

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

scheme2

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

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

我使用的最简单的解决scheme是:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:TRUE]; } 

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

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

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

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

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

代码:

在viewDidLoad中

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

在closures键盘:

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

你需要添加一个透明的UIVIew作为键盘下面的子视图,并在那里处理触摸,closures键盘。 下面的代码供您参考。

 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]; 

其他的方法很简单:

UIView as UIControl in custom class in the interface builderUIView as UIControl in custom class in the interface builder ,然后在UIView : UIControl Touch up inside event中添加一个IBAction方法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]; } 

然后,你有其他的select,它是把你的文本框the return key of the keyboard as Done (它可以是任何你想要的,但完成它是有利的,因为返回意味着做一个forms的行动)在界面生成器,所以你可以按Done键并隐藏键盘,但是在这种情况下,你必须把上一个IBAction方法附加到Did end on exit事件的Did end on exit

这样键盘就会隐藏在键盘touching outside或触摸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语法。

这是一个Swift 4解决scheme:

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

而closures键盘

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