Freelance iOS Developer, Rapidly Aging Punk

Tap Anywhere to Dismiss the iOS Keyboard

Nov 13, 2014 at 12:39PM

This is something so common I feel like it should be a built-in feature. It isn't though, so here's how to implement it yourself. If your app uses a UINavigationController a custom subclass of that is a good place to use this.


@interface RootNavigationController ()
{
    UIGestureRecognizer *tapRecognizer;
}

@end

@implementation RootNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];

    tapRecognizer = [[UITapGestureRecognizer alloc]
        initWithTarget:self
                action:@selector(dismissInputViews)];

    [tapRecognizer setCancelsTouchesInView:FALSE];
    [self.view addGestureRecognizer:tapRecognizer];
}

-(void)dismissInputViews
{
    [self.view endEditing:YES];
}

@end

That's it.