Question

I have a UISearchBar which when clicked, shows the keyboard. However if the user presses the home button while the keyboard is shown and then goes back into the app, the keyboard is still visible. How can I hide th keyboard when the app closes/enters background?

I've tried the following in viewDidDisappear:

[eventSearchBar resignFirstResponder];

[eventSearchBar endEditing:YES];

I've also tried this in the delegate in appDidEnterBackground:

[self.rootController.navigationController.view endEditing:YES];

None of these worked.

Was it helpful?

Solution

you can do this in appDelegate....

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.window endEditing:YES];
}

OTHER TIPS

Swift version of this:

func applicationDidEnterBackground(application: UIApplication) {
    window?.endEditing(true)
}

In your view controller, for example in the init method, register for the UIApplicationWillResignActiveNotification:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(willResignActive:)
    name:UIApplicationWillResignActiveNotification
    object:nil];

When the app goes into background, make the search display controller inactive. This removes the focus from the search field and hides the keyboard:

- (void)willResignActive:(NSNotification *)note
{
    self.searchDisplayController.active = NO;

    // Alternatively, if you only want to hide the keyboard:
    // [self.searchDisplayController.searchBar resignFirstResponder];
}

And don't forget to remove the observer in the dealloc method:

[[NSNotificationCenter defaultCenter] removeObserver:self
    name:UIApplicationWillResignActiveNotification
    object:nil];

I've had all standard methods fail on my sporadically. So far this is the only way I've gotten solid results.

In topmost controller.

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil];
}

-(void) willResignActiveNotification:(NSNotification*) vNotification {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [self setEditing:NO];
}

There is an odd case where a text field will no longer respond to resignFirstResponder or endEditing but still has keyboard up.

Solution in swift 3.2 Version

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(hideTextField), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    func hideTextField(){
        eventSearchBar.endEditing(true)
    }

The best way is to put window?.endEditing(true) in applicationWillResignActive on AppDelegate:

func applicationWillResignActive(_ application: UIApplication) {
    window?.endEditing(true)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top