resignFirstResponder not hiding keyboard on textFieldShouldReturn

后端 未结 14 1691
我在风中等你
我在风中等你 2020-12-05 10:11

I have a view with a UITextField which should hide the keyboard when return is pressed.

My function is this:

- (BOOL)textFieldShouldReturn:(UITextFie         


        
相关标签:
14条回答
  • 2020-12-05 10:21

    I see you have the iPad tag on this. Do you happen to be presenting a modal view using UIModalPresentationFormSheet? If so, it looks like this is a limitation of the FormSheet modal presentation (either Apple is doing it intentionally for some reason, or it is a bug). See these other questions for more details:

    Modal Dialog Does Not Dismiss Keyboard

    Modal View Controller with keyboard on landscape iPad changes location when dismissed

    0 讨论(0)
  • 2020-12-05 10:23

    The easiest way is:

    1. Go to your user interface builder,

    2. select UITextField and "Control-Drag" to "Detail View Controller-Detail" and release.

    3. The window will pop-up. Then under "Outlets" select "Delegate".

    That's it. It worked for me.

    0 讨论(0)
  • 2020-12-05 10:28

    To deal with the bug mentioned by Brandon, you can try closing and re-opening your modal view controller as long as you still have a reference to it.

    [textField resignFirstResponder];
    [self dismissModalViewControllerAnimated:NO];
    [self presentModalViewController:yourModalViewControllerReference animated:NO];
    

    (where "self" should be the controller you used to originally open the modal view controller)

    0 讨论(0)
  • 2020-12-05 10:31

    Based on your comment that it looks like focus has shifted, then I think what may be happening is that the keyboard is staying open for the next text input field. If your return key is a "Next" key, then returning YES for textFieldShouldReturn: will make the next textField the first responder, and keep the keyboard visible.

    0 讨论(0)
  • 2020-12-05 10:32

    This solution worked for me after none of the above did. after calling resignFirstResponder i added a modal view & removed it.

    
    
        [myTextField resignFirstResponder];
        UIViewController *dummyController = [[UIViewController alloc] init];
        UIView *dummy = [[UIView alloc] initWithFrame:CGRectMake(-1, -1,1,1)];
        [dummyController setView:dummy];
        [self presentModalViewController:dummyController animated:NO];
        [dummyController dismissModalViewControllerAnimated:NO];
    
    
    0 讨论(0)
  • 2020-12-05 10:32

    Swift 3.0:

    override var disablesAutomaticKeyboardDismissal: Bool {
        get{
            return false
        }
        set {
            self.disablesAutomaticKeyboardDismissal = false
        }
    }
    
    0 讨论(0)
提交回复
热议问题