Search code examples
uinavigationbaruisearchbar

UISearchBar below navigation bar, how to make it NOT push nav bar up when active?


Touching the search bar in an apple app I am developing causes the navigation bar to be hidden.

Here are two screenshots to demonstrate:

screenshot of an apple phone screen, showing a blank app screen with 'Search' written on the navigation bar. A search box is displayed below this navigation bar

screenshot of an apple phone screen, showing an open search box, with cursor. There is a cancel button to the right of the search box, and a keyboard is displayed in the bottom half of the screen

When you tap on the search bar, it pushes the navigation bar up, off the top of the screen. How can I make it stay in place when clicking search?

Thanks!


Solution

  • One method is to prevent the searchBar from accessing the navigationBar - implement

    - (UINavigationController *)navigationController {
        return nil;
    }
    

    in the viewController assigned as the UISearchBar's delegate (or UISearchDisplayController's delegate).

    If you still need to access the navigationController elsewhere in the controller, use

    super.navigationController
    

    in place of

    self.navigationController