【问题标题】:How can I dismiss the keyboard if a user taps off the on-screen keyboard?如果用户点击屏幕键盘,我如何关闭键盘?
【发布时间】:2011-08-08 08:38:41
【问题描述】:

我希望能够在用户点击键盘外的任意位置时关闭 iPhone 键盘。我该怎么做呢?我知道我需要解除响应者,但需要知道当用户敲击键盘空间时如何实现它。

【问题讨论】:

标签: iphone objective-c cocoa-touch ios uitextfield


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    代码:

    viewDidLoad

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

    dismissKeyboard:

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

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

    选项 2

    如果你负担不起添加手势识别器的费用,那么你可以试试这个

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

    【讨论】:

    • 带有手势识别器的非常优雅的解决方案!
    • 这很好,但是,手势动作正在吞噬我在视图上的所有按钮单击事件。有什么想法吗?
    • 在 ios 中关闭键盘的好方法。
    • 我认为这很棒,但它也覆盖了任何其他输入,如 CollectionViews。请参阅下面的 Matt Rees 的评论以进行修复。
    • 在 Swift 中使用 touches.first 代替 [touches anyObject]
    【解决方案3】:

    我用过的最简单的解决方案是这样的:

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

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

    根据Apple的文档,我认为这个方法是专门用来解决这个问题的。

    【讨论】:

    • UIScrollView 怎么样?
    • 这是最好的解决方案,不用怀疑!
    • 试试这个 UIScrollView scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    【解决方案4】:

    添加一个 tapGesture 识别器,但确保 cancelsTouchesInView = NO

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

    【讨论】:

    • 关于 tapGesture.cancelsTouchesInView = NO;非常感谢!
    【解决方案5】:

    其他方法很简单:

    创建你的UIView as UIControl in custom class in the interface builder,然后你可以在UIView : UIControlTouch up inside event 中附加一个IBAction 方法,然后你将[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];
    }
    

    然后,您还有其他选择,那就是在界面中输入您的文本字段the return key of the keyboard as Done(可以是您想要的任何一个,但完成它对此有好处,因为返回意味着对表单执行操作) builder,因此您可以按Done 并隐藏键盘,但在这种情况下,您必须将先前的 IBAction 方法附加到Did end on exit 事件。

    通过这种方式,键盘将隐藏touching outside 或从键盘上触摸Done

    如果你想改进代码,如果只是将键盘触摸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];
    }
    

    【讨论】:

      【解决方案6】:

      如果您使用UITableViewController 或有用户将与之交互的UITableView,则在您的 ViewDidLoad 中您可以简单地执行以下操作:

      tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;
      

      注意:这是 Xamarin.iOS 语法。

      【讨论】:

        【解决方案7】:

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

        代码:

        在 viewDidLoad 中

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

        在dismissKeyboard中:

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

        【讨论】:

          【解决方案8】:

          这是一个 Swift 4 解决方案:

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

          还有dismissKeyboard

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

          【讨论】:

            【解决方案9】:

            如果您不想添加手势识别器,也可以使用 touchesBegin 方法

            Swift 4 中的代码

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

            【讨论】:

              猜你喜欢
              • 2017-01-15
              • 2019-01-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多