当用户点击文本框外的其他区域时如何closures键盘?

今天我试图在我的iPod(iOS 6.1.3)上运行我的代码,并在这里发现了一些有趣的事情…

首先,当我点击文本框时,键盘显示出来,但当我点击文本框外的其他位置时,它不会隐藏。

所以我决定谷歌search,并find这个解决scheme:

_fieldEmail.delegate = self; _fieldEmail.returnKeyType = UIReturnKeyDone; _fieldPassword.delegate = self; _fieldPassword.returnKeyType = UIReturnKeyDone; _fieldRegisterName.delegate = self; _fieldRegisterName.returnKeyType = UIReturnKeyDone; _fieldRegisterEmail.delegate = self; _fieldRegisterEmail.returnKeyType = UIReturnKeyDone; _fieldRegisterPassword.delegate = self; _fieldRegisterPassword.returnKeyType = UIReturnKeyDone; 

它的作品…它给键盘底部的“完成”button,现在键盘可以通过按下隐藏。

但我在这里有两个问题:

  1. 当“DONE”button被点击时,键盘只会隐藏。 而不是通过点击文本框外的其他区域。 我不知道这是否正常在iOS世界,但通常我看到很多应用程序不这样做。
  2. 有没有什么办法来循环这个过程,所以我没有手动添加一个所有的文本字段,我有吗? 怎么做?

这就是我所需要知道的

下面的代码将适用于所有UITextField的UIView中的所有组件

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UIView * txt in self.view.subviews){ if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) { [txt resignFirstResponder]; } } } 

要么

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } 
  1. 只需将UITapGestureRecogniser添加到您的视图中即可调用resignFirstResponder

在viewDidLoad中:

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

接着 :

 -(void)hideKeyBoard { [yourTextField resignFirstResponder]; } 

2.您可以inheritanceUITextField,但是除非您有1000个textField,否则可以像现在这样做。

这是针对Xcode 6.0.1的Swift编程

 override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:") tapRecognizer.numberOfTapsRequired = 1 self.view.addGestureRecognizer(tapRecognizer) } func handleSingleTap(recognizer: UITapGestureRecognizer) { self.view.endEditing(true) } 

使用任一

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

和方法的代码

 -(void)handleSingleTap:(UITapGestureRecognizer *)sender{ [self.TextFiledName resignFirstResponder]; } 

或_和最好的其他选项是

只需添加

  [self.view endEditing:YES]; 

而且,当你从任何angular度轻轻一击,键盘就会隐藏起来:)

closures键盘

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

我通常做的是打电话

 [field resignFirstResponder]; 

从一个看不见的button。 我相信这是一个更好的方法来做到这一点。 我已经在一个应用程序中完成了这一段时间。

简单,

使用IQKeyboardManager并将这一行代码放在AppDelegate.m中

 [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES]; 

以下是我如何做到这一点:

在myView的.h文件中:

 @property (strong) UITextField * savedTextField; 

在myView的.m文件中:

 @implementation myView @synthesize savedTextField; 

在myView的init例程中:

 [self setSavedTextField: nil]; 

在myView的textField委托区中,我保存了当前正在激活的textField的id:

 - (BOOL) textFieldShouldBeginEditing: (UITextField *) textField { [self setSavedTextField: textField]; . . . return( YES ); } 

在myView的例程中,当访问其他控件时,我的代码会查看其他控件的所有stream量,如果有一个是textField,它会调用它来调用FirstResponder状态,然后删除nil到savedTextField属性中:

 - (void) handleCtrlEvents: (UIControl *) aCtrl { if ( [self savedTextField] != nil ) { [[self savedTextField] resignFirstResponder]; [self setSavedTextField: nil]; } . . . } 

这对我来说干净利落。

我遇到了同样的问题。 通常你想要的是,无论你点击你的应用程序(显示在你的主/当前UIView),而不是在键盘上closures它。 在networkingsearch一个聪明的解决scheme和testing几个,我认为这是最好的扩展/子类/实现UIGestureRecognizerDelegate

由于许多控件(如UITableView等)可以响应手势(select,滚动等),并且如果只是将UITapGestureRecognizer添加到整个(main-)ViewController的视图来closures键盘,用户体验并不是最好的,因为用户需要点击两次,如果他不打算closures键盘,但是例如从应用程序的当前“主”视图内的UITableView中select一行。

由于我对整个iOS和Swift的东西都比较陌生, Swift代码的例子很less见,下面是我的全键盘控件:

 class ViewController: UIViewController, UIGestureRecognizerDelegate { override func viewDidLoad(){ self.initializeCloseKeyboardTap() } func initializeCloseKeyboardTap() { NSNotificationCenter.defaultCenter().addObserver(self, selector: "onKeyboardOpen:", name: UIKeyboardDidShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "onKeyboardClose:", name: UIKeyboardDidHideNotification, object: nil) let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleOnTapAnywhereButKeyboard:") tapRecognizer.delegate = self //delegate event notifications to this class self.view.addGestureRecognizer(tapRecognizer) } func onKeyboardClose(notification: NSNotification) { println("keyboardClosed") } func onKeyboardOpen(notification: NSNotification) { println("keyboardOpen") } func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { self.view.endEditing(true) return false } 

你可以将它embedded到ViewController中,并在viewDidLoad实现中调用initiallizeCloseKeyboardTa()。它还实现了一个通知观察者,当通过NSNotificationCenter UIKeyboardDidShowNotification和UIKeyboardDidHideNotification打开或closures键盘(完成)

希望节省一些时间的他人:-)

| * | closuresUITextField的键盘:重写方法touchesBegan:withEvent:对于ViewController

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

|和|

 override func viewDidLoad() { super.viewDidLoad() // For tapping outside text box self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(DismisKeyPadFnc))) } // For tapping outside text box func DismisKeyPadFnc() { view.endEditing(true) } 

| * | 在个人文本框上closuresUITextField的键盘:

 class NamVcc: UIViewController, UITextFieldDelegate { @IBOutlet var NamTxtBoxVid: UITextField! override func viewDidLoad() { super.viewDidLoad() // For hitting return key NamTxtBoxVid.delegate = self // For tapping outside text box self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(DismisKeyPadFnc))) } /* For hitting return key */ func textFieldShouldReturn(NamTxtBoxPsgVid: UITextField) -> Bool { NamTxtBoxPsgVid.resignFirstResponder() return true } /* For tapping outside text box */ func DismisKeyPadFnc() { view.endEditing(true) } } 

| * | 实现下一步button单击:
1)您将需要在xib / Storyboard或代码中的文本字段的增量序列中设置标签。
2)为每个文本字段设置代理。
3)然后将以下代码粘贴到您的视图控制器中,以获得所需的效果:

 func textFieldShouldReturn(TxtBoxPsgVar: UITextField) -> Bool { let NxtTxtBoxTagVal : Int = TxtBoxPsgVar.tag + 1 let NxtTxtBoxVal: UIResponder? = TxtBoxPsgVar.superview?.superview?.viewWithTag(NxtTxtBoxTagVal) if let TxtBoxVal = NxtTxtBoxVal { // Found next responder, so set it. TxtBoxVal.becomeFirstResponder() } else { // Not found, so remove keyboard. TxtBoxPsgVar.resignFirstResponder() } return true } 

我认为

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

是方便的,但不是最好的解决scheme。 和TapGestureRecongnizer更好,但很难使用,你必须设置代表,并添加和删除Recongnizer。 所以我写了一个简单的子类,可以很容易地使用:

 class TapHideTextField: UITextField { override func awakeFromNib() { super.awakeFromNib() tapGr = UITapGestureRecognizer(target: self, action:"tap") NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBeginEditing", name:UITextFieldTextDidBeginEditingNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEndEditing", name:UITextFieldTextDidEndEditingNotification, object: nil) } deinit{ NSNotificationCenter.defaultCenter().removeObserver(self) } var tapGr:UITapGestureRecognizer! func tap(){ self.endEditing(true) } func didBeginEditing(){ self.superview!.addGestureRecognizer(tapGr) } func didEndEditing(){ self.superview!.removeGestureRecognizer(tapGr) } 

}

你应该关心superview和cancelTouchesInView属性。

或在GitHub: https : //github.com/lilidan/TapHideText/blob/master/TapHideTextField.swift

 -(void) textFieldDidBeginEditing: (UITextField *) textField{ [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DismissKeyboard:)]]; } -(void) DismissKeyboard:(UITapGestureRecognizer *) sender{ [self.view endEditing:YES]; [self.view removeGestureRecognizer:sender]; }