How to hide the keyboard programmatically in iphone

后端 未结 6 1999
春和景丽
春和景丽 2021-01-11 13:43

How to hide the keyboard programmatically in iphone?

相关标签:
6条回答
  • 2021-01-11 13:50
    [textFieldName resignFirstResponder];
    
    0 讨论(0)
  • 2021-01-11 13:53

    Tell the UIResponder subclass that is currently first responder to resign its first responder status:

    [responder resignFirstResponder];
    
    0 讨论(0)
  • 2021-01-11 13:59

    Call this in your ViewController

    [self.view endEditing:YES];
    
    0 讨论(0)
  • 2021-01-11 14:01

    If you are using textview then

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

    and if you are using textfield then

    -(BOOL)textFieldShouldReturn:(UITextField*)textField;
     {
    
    [textField resignFirstResponder];
    
     }
    
    0 讨论(0)
  • 2021-01-11 14:05

    Here is the swift version:

    UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
    
    0 讨论(0)
  • 2021-01-11 14:06

    It's easy:

    ObjC

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

    Swift

    UIApplication.shared.keyWindow?.endEditing(true)
    

    take a look at UIView Class Reference for endEditing. Causes the view (or one of its embedded text fields) to resign the first responder status. And keyWindow is the only window that receives keyboard events, so this solution is guarantied to always work.

    0 讨论(0)
提交回复
热议问题