Search code examples
ipadkeyboarduitextfielddismissuimodalpresentationstyle

How to dismiss the keyboard on iPad when using modalPresentationStyle


For my iPad app, I have a view displayed modally as a formsheet when a button is pushed. In order to have the keyboard dismissed after entering text in a textfield i tried as suggested;

the "disablesAutomaticKeyboardDismissal" method.

This does not work, in fact, the method is never called acording to the log. The keybord will dismiss for iPhone or when i choose to not present modally.

Here is my code:

- (BOOL)disablesAutomaticKeyboardDismissal
{  
    NSLog(@"method calls");
    return NO;
}

- (IBAction)showNewView:(id)sender
{

    MyViewController *mvc = 
            [[MyViewController alloc] init];

// some lines about setting content
//...

    UINavigationController *navController = [[UINavigationController alloc] 
                                initWithRootViewController:mvc];

    [navController setModalPresentationStyle:UIModalPresentationFormSheet];
    [self presentViewController:navController animated:YES completion:nil];
}

-(BOOL)disablesAutomaticKeyboardDismissal or not, the keyboard is not dismissed unless i remove tis line:

    // [navController setModalPresentationStyle:UIModalPresentationFormSheet];

However, then it is not presented the way I want anymore.

Can anyone see what I am doing wrong?


Solution

  • -(BOOL)disablesAutomaticKeyboardDismissal needs to overridden to return NO by the view controller that is presented as a form sheet, not by the presenter; That's your mistake. In your case you could subclass UINavigationController to get the desired behaviour:

    @interface AutomaticKeyboardDismissingNavigationController : UINavigationController
    @end
    
    @implementation AutomaticKeyboardDismissingNavigationController
    - (BOOL)disablesAutomaticKeyboardDismissal
    {
        return NO;
    }
    @end
    

    (The class name could probably be a bit shorter and still be comprehensible.)