点击任何 UIControl 对象时关闭键盘 [英] dismiss keyboard on tapping on any UIControl objects

查看:30
本文介绍了点击任何 UIControl 对象时关闭键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个键盘触发元素...UITextFieldUITextViewUISearchBar.

I have multiple keyboard provoking elements...UITextField, UITextView and UISearchBar.

我想在触摸除键盘和当前活动的文本编辑"元素之外的任何地方时关闭 UIKeyboard.

I would like to dismiss UIKeyboard upon touch anywhere except keyboard and currently active "text editing" element.

我已经实施了

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

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

This works if user taps on an "inert" element...background or userDisabledView... but most of my View is made up of "active" elements like UITableView and UIButtons...

有没有办法让这一切成为可能,无论在何处进行点击.

Is there a way to make this possible, regardless where the tap is made.

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

The only way I know of is to use a large invisible button that slides over the view whenever UIKeyboard is presented, which calls self.view.endEditing(true), then retracts to offscreen.

有什么帮助吗?

推荐答案

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

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

First in your viewDidLoad, add keyboard notification:-

// 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 值,以设置一个值是否存在键盘

Now set a BOOL value , to set a value whether keyboard is present or not

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

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

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

Now, add tap gesture to all your controls

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

这篇关于点击任何 UIControl 对象时关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆