How do I dismiss the iOS keyboard?

后端 未结 16 1013
情书的邮戳
情书的邮戳 2020-11-29 20:00

I have a UITextfield that i\'d like to dismiss the keyboard for. I can\'t seem to make the keyboard go away no matter what code i use.

相关标签:
16条回答
  • 2020-11-29 20:09
    1. Set up the "Did End On Exit" event in Xcode (right click on your text field).

    2. Realize this method:

      -(IBAction) closeKeyboard:(id) sender {
          [_txtField resignFirstResponder];
      }
      
    0 讨论(0)
  • 2020-11-29 20:14

    I've discovered a case where endEditing and resignFirstResponder fail. This has worked for me in those cases.

    ObjC

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];    
    [self setEditing:NO];
    

    Swift

    UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
    
    0 讨论(0)
  • 2020-11-29 20:16

    I suggest you add and action on your header file:

    -(IBAction)removeKeyboard;
    

    And in the implementation, write something like this:

    -(IBAction)removeKeyboard
    {
    [self.textfield resignFirstResponder];
    }
    

    In the NIB file, connect from the UITextFiled to the File's Owner on the option DidEndOnExit. That way, when you press return, the keyboard will disappear.

    Hope it helps!

    0 讨论(0)
  • 2020-11-29 20:18

    This will resign one particular text field

    // Swift
    TextField.resignFirstResponder()
    
    // Objective C
    [TextField resignFirstResponder];
    

    To resign any text field use below code

    // Swift
    self.view!.endEditing(true)
    
    // Objective C
    [self.view endEditing:YES];
    
    0 讨论(0)
  • 2020-11-29 20:22

    If you don't know which textField is the first responder you can find it. I use this function:

    UIView *resignFirstResponder(UIView *theView)
    {
        if([theView isFirstResponder])
        {
            [theView resignFirstResponder];
            return theView;
        }
        for(UIView *subview in theView.subviews)
        {
            UIView *result = resignFirstResponder(subview);
            if(result) return result;
        }
        return nil;
    }
    

    Then in your code call:

        UIView *resigned = resignFirstResponder([UIScreen mainScreen]);
    
    0 讨论(0)
  • 2020-11-29 20:23

    3 Simple & Swift steps

    Add UITextFieldDelegate to your class as below:

    class RegisterVC: UIViewController, UITextFieldDelegate {
        //class implementation 
    }
    

    in class implementation, add the delegate function textFieldShouldEndEditing::

    internal func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }
    

    and as a last step, set your UITextField(s) delegate(s) to self, in somewhere appropriate. For example, inside the viewDidLoad function:

    override func viewDidLoad(){
        super.viewDidLoad()
        myTextField1.delegate = self
        myTextField2.delegate = self
        ..
        ..
    }
    

    Now, whenever user hits the return key, keyboard will dismiss.

    I prepared an example snippet too, you can check it from here.

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