IOS Hide the navigation bar when the onscreen keyboard appears
The iOS navigation bar is hidden when the on-screen keyboard is activated in my app. How can I prevent this? This happens when the user is in the search bar and also if the user clicks on it.
I was able to display the navigation bar on the search page as soon as the user clicks on cancel or search / complete editing, but the search bar then gets under the navigation bar.
I do not have a "hidden panel if keyboard appears" selected in the interface builder.
+3
source to share
3 answers
Try it,
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
source to share
> 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
source to share