How to HIDE the iPad keyboard from a MODAL view controller?

后端 未结 5 1555
离开以前
离开以前 2020-12-14 18:05

I\'m trying to hide the iPad keyboard from a modal view controller but it doesn\'t work. I have tried resignFirstResponder but that doesn\'t have any affect if we are in a m

5条回答
  •  既然无缘
    2020-12-14 18:26

    This was a total pain to find. Seems like one of the poorer API designs in iOS. Much appreciation to @0xced and @manicaesar for the answers.

    Here's my consolidated answer for future devs who are stuck beating their head against the wall.

    If it's a single view controller, just override disablesAutomaticKeyboardDismissal and return NO.

    If it's a navigation controller in a modal, create your own UINavigationController subclass like so:

    In .h...

    
    @interface MyNavigationController : UINavigationController
    
    @end
    

    In .m....

    @implementation MyNavigationController
    
    
    #pragma mark -
    #pragma mark UIViewController
    - (BOOL)disablesAutomaticKeyboardDismissal {
        return NO;
    }
    
    @end
    

    In your code that shows a modal view controller.

    UIViewController *someViewController = [[UIViewController alloc] init];
    
    MyNavigationController *navController = [[MyNavigationController alloc] initWithRootViewController:someViewController];
    
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:navController animated:YES];
    
    

提交回复
热议问题