iOS Hiding navigation bar when on screen keyboard appears

3.8k Views Asked by At

The iOS navigation bar is hidden when the on-screen keyboard is activated within my application. How can I prevent this from happening? It happens when the user is in the search bar and also if the user clicks in it.

I managed to show the navigation bar on the search page once the user clicks cancel or search/finish editing but the search bar then goes under the navigation bar.

I do not have "hide bar if keyboard appears" selected on the interface builder.

enter image description here

enter image description here

3

There are 3 best solutions below

2
On

Try this,

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShowNotification:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHideNotification:", name: UIKeyboardWillHideNotification, object: nil)
}


override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}

 func keyboardWillShowNotification(notification: NSNotification) {
    self.navigationController?.navigationBarHidden = false
}

func keyboardWillHideNotification(notification: NSNotification) {
    self.navigationController?.navigationBarHidden = false
}
0
On
>  NSDictionary* info = [note userInfo];
>         CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
>         UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+80, 0.0);
>         _scrollBackground.contentInset = contentInsets;
>         _scrollBackground.scrollIndicatorInsets = contentInsets;
0
On

You can also just go on Xcode and uncheck:

Hide Bars -> "When keyboard Appears" and that works fines.

enter image description here