Dave
Dave

Reputation: 783

iPhone: Hide Keyboard on App Did Enter Background or View Did Disappear

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.

Upvotes: 17

Views: 9674

Answers (6)

Fabio
Fabio

Reputation: 5628

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

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

Upvotes: 1

Sudhi 9135
Sudhi 9135

Reputation: 765

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)
    }

Upvotes: 1

Esqarrouth
Esqarrouth

Reputation: 39181

Swift version of this:

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

Upvotes: 7

Andres Canella
Andres Canella

Reputation: 3716

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.

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31081

you can do this in appDelegate....

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

Upvotes: 43

Martin R
Martin R

Reputation: 539795

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];

Upvotes: 4

Related Questions