【问题标题】:How to hide the keyboard programmatically in iphone如何在 iphone 中以编程方式隐藏键盘
【发布时间】:2011-02-04 22:16:27
【问题描述】:

如何在 iphone 中以编程方式隐藏键盘?

【问题讨论】:

    标签: ios iphone iphone-softkeyboard


    【解决方案1】:

    告诉当前是第一响应者的 UIResponder 子类放弃其第一响应者状态:

    [responder resignFirstResponder];
    

    【讨论】:

      【解决方案2】:
      [textFieldName resignFirstResponder];
      

      【讨论】:

        【解决方案3】:

        在你的 ViewController 中调用它

        [self.view endEditing:YES];
        

        【讨论】:

          【解决方案4】:

          很简单:

          ObjC

          [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
          

          斯威夫特

          UIApplication.shared.keyWindow?.endEditing(true)
          

          看看UIView Class Reference for endEditing。 导致视图(或其嵌入的文本字段之一)放弃第一响应者状态。而keyWindow 是唯一接收键盘事件的窗口,所以这个解决方案可以保证一直有效。

          【讨论】:

            【解决方案5】:

            如果你使用的是 textview 那么

            - (BOOL)textView:(UITextView *)textView
             shouldChangeTextInRange:(NSRange)range
            replacementText:(NSString *)text
            {
              if ([text isEqualToString:@"\n"])
             {
                [textView resignFirstResponder];
                [self keyboardWillHide];
             }
            }
            

            如果您使用的是文本字段,那么

            -(BOOL)textFieldShouldReturn:(UITextField*)textField;
             {
            
            [textField resignFirstResponder];
            
             }
            

            【讨论】:

              【解决方案6】:

              这里是快速版本:

              UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-12-06
                • 2016-06-25
                • 1970-01-01
                相关资源
                最近更新 更多