Ios – Activating search bar doesn’t hide navigation bar

iosios7objective cuisearchbarxcode

I have an iOS 7 application in XCode 5.0 that exhibits some strange behavior when tapping the search bar (UISearchBar).

My application has a Navigation Controller, and a Tab Bar Controller. Here is an example of what my Main.Storyboard looks like:

[Navigation Controller] -> [Tab Bar Controller] -> [Tab Item #1]
                                    |
                                    -------------> [Tab Item #2]

Each [] is a view controller

When I launch my application, I see the Tab Item 1 with the UISearchBar as shown in the screenshot below:

one

When I tap the UISearchBar, the search bar slides up to the top of the screen, but the Navigation Bar does not hide, and the view does not "slide up". This causes the app to look like this:

two

When I delete the Tab Bar Controller from the storyboard and connect the Navigation Controller directly to Tab Item #1 the Navigation Bar hides as expected.

How can I make the Navigation Bar hide when tapping the Search Bar? For an example of the functionality I am looking to reproduce, click the search bar under the "Contacts" tab of the default iOS7 "Phone" application.

Best Answer

For swift developers:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { 

  navigationController?.setNavigationBarHidden(true, animated: true)

}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { 

  navigationController?.setNavigationBarHidden(false, animated: true)

}

This will hide the navigation bar while search bar is active and show it again when search bar is inactive.

Related Topic