在点击任何 UIControl 对象时关闭键盘

dismiss keyboard on tapping on any UIControl objects

我有多个键盘挑衅元素...UITextFieldUITextViewUISearchBar

我想在触摸除键盘和当前活动的 "text editing" 元素以外的任何地方时关闭 UIKeyboard

我已经实现了

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    searchBar.resignFirstResponder()
    name.resignFirstResponder() //UITextField
    notes.resignFirstResponder() //UITextView
    self.view.endEditing(true)
}

如果用户点击 "inert" 元素...背景或 userDisabledView...这会起作用,但我的大部分视图由 "active" 元素组成,例如 UITableViewUIButtons...

有没有一种方法可以实现这一点,无论在何处进行点击。


我知道的唯一方法是使用一个大的不可见按钮,每当出现 UIKeyboard 时,它就会在视图上滑动,调用 self.view.endEditing(true),然后缩回到屏幕外。

有什么帮助吗?

注意:我通过在 didSelectRowAtIndexPath 中实现 self.view.endEditing(true) 解决了如果 tap 在 UITableView 上的问题,仍然想听听其他方法

您可以为视图内的每个控件附加点击手势, tapGestRecog.cancelsTouchesInView=NO 防止点击识别器成为唯一一个捕捉所有点击然后在 tapAction 上退出你的键盘。

首先在你的viewDidLoad中,添加键盘通知:-

// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

现在设置一个 BOOL 值,以设置一个值是否存在键盘

- (void)keyboardDidShow: (NSNotification *) notif{
     isKeyboardPresent=TRUE;
    // Do something here
}

- (void)keyboardDidHide: (NSNotification *) notif{
     isKeyboardPresent=False;
    // Do something here
}

现在,为所有控件添加点击手势

for(UIView *vw in [self.view subviews])
     {
         UITapGestureRecognizer *tapGestRecog = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(controlTapAction)];
         tapGestRecog.delegate=self;
         [tapGestRecog setNumberOfTapsRequired:1];
         tapGestRecog.cancelsTouchesInView = NO;
         [vw addGestureRecognizer:tapGestRecog];

     }
-(IBAction) controlTapAction
{
  if(isKeyboardPresent)
   {
       [self.view endEditing:TRUE];
  } 
}

这对我一直有效......!!

override func touchesBegan(touches: Set, withEvent event: UIEvent) {

    self.view.endEditing(true )

}

//currently active "text editing" element.
var firstResponderView : UIView?

var tap: UITapGestureRecognizer?

//cover the currently active "text editing" element.
var smallView:UIView?

//dont't cover the keyboard
var bigView: UIView?

func createView(){
    bigView = UIView(frame: CGRectZero)
    bigView!.userInteractionEnabled=false

    tap = UITapGestureRecognizer(target: self, action: "endEditing")

    smallView = UIView(frame: CGRectZero)
    smallView!.userInteractionEnabled=false
    bigView!.addSubview(smallView!)
    self.view.addSubview(bigView!)
    self.update()
}

func endEditing(){
    self.view.endEditing(true)
    self.update()
}

//call this after any view become first responder or resign
func update() {

    if(firstResponderView?.isFirstResponder()==true)
    {
        bigView!.addGestureRecognizer(tap!)
        bigView!.userInteractionEnabled = true
        smallView!.frame = firstResponderView!.frame
    }
    else{
        bigView!.removeGestureRecognizer(tap!)
        bigView!.userInteractionEnabled = false
        smallView!.frame=CGRectZero
    }
}

您想在活动控件点击时关闭键盘,为此您必须使用

resignFristResponder()

在每个活动控件上 就像你有一个 UIButton 一样,你必须把这个方法放在 buttonClick 动作上。像前任

  @IBAction func nextButtonTapped(sender: AnyObject)
{

    txtlang.resignFirstResponder()
}

这是唯一的例子。 希望对你有帮助

编辑 1 = 您也可以按原样使用 endEditing() 方法。 **我的英语非常糟糕。抱歉。