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

时间:2015-05-22 04:56:49

标签: ios swift uikeyboard

我有多个发起键盘的元素...... UITextFieldUITextViewUISearchBar

除了键盘和当前有效的"文本编辑"我想触摸除UIKeyboard以外的任何地方。元件。

我已实施

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

如果用户点击&#34; inert&#34; element ... background或userDisabledView ...但我的大部分视图都由&#34; active&#34;组成。像UITableViewUIButtons ...

这样的元素

有没有办法使这成为可能,无论在何处进行点击。

我知道的唯一方法是使用一个隐藏在视图上的大隐形按钮,只要显示UIKeyboard,就会调用self.view.endEditing(true),然后缩回到屏幕外。

任何帮助?

注意:如果点击是在UITableView上我解决了,通过在self.view.endEditing(true)中实施didSelectRowAtIndexPath,仍然希望听到其他方法

4 个答案:

答案 0 :(得分:1)

这总是为我工作..... !!

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

var inlineEditOptions = {
        extraparam: {
            environment: function () {
                return $(".drg_drop_one select").val();
            }
        }
    };

jQuery("#sortrows").jqGrid('inlineNav', '#psortrows', {
    add: true,
    addtext: "Add",
    addicon:"ui-icon-plus",
    edit: true,
    editicon: "ui-icon-pencil",
    edittext: "Edit",
    save: true,
    saveicon: "ui-icon-disk",
    savetext:"Save",
    cancel: true,
    cancelicon: "ui-icon-cancel",
    canceltext: "Cancel",
    search: true,
    searchtext: "Seaech",                
    // additional parameters below:
    editParams: inlineEditOptions,
    addRowParams: {addParams: inlineEditOptions }
});

}

答案 1 :(得分:1)

//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
    }
}

答案 2 :(得分:1)

你想点击活动控件上的keybord,因为你必须使用

resignFristResponder()

在每个活动控件上 就像你有一个UIButton而不是你必须把这个方法放在buttonClick Action上。像前 -

  @IBAction func nextButtonTapped(sender: AnyObject)
{

    txtlang.resignFirstResponder()
}

唯一的例子。 希望它能帮到你

编辑1 =您也可以使用endEditing()方法。 **我的英语非常糟糕。对不起。

答案 3 :(得分:0)

您可以为视图中的每个控件附加点击手势, 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];
  } 
}