Keeping UINavigationController's navigationBar hidden after UISearchDisplayController selection

peppy picture peppy · Feb 26, 2010 · Viewed 8k times · Source

I have a UISearchDisplayController setup with a UITableViewController which is nested inside a UINavigationController. When a selection of a cell is made, UITableView's didSelectRowAtIndexPath method is triggered, which pushes a new view to the parent navigation controller. This new view should have the navigation bar hidden on entry.

[[self navigationController] setNavigationBarHidden:YES animated:NO];

I use this line in the didSelectRowAtIndexPath method to hide the navigation bar. This works fine when a row is selected not using the search controller, but is overridden when selecting a search result. It seems the UISearchDisplayController takes it in its right to un-hide the navigationBar sometime after the row is selected.

If I move the setNavigationBarHidden call into the target view's viewWillAppear method, results are similar. I can make it work by placing the hide call in viewDidAppear, but this makes for a very awkward transition effect which feels jumpy and out of place. I would like to make the navigationBar already hidden before the new view slides on to the screen.

Does anyone know where the unhiding of the navigationBar is occurring, and/or any way I can override this behaviour?

Answer

SMSidat picture SMSidat · Jun 14, 2011

This may not be the most elegant solution, but I believe it does exactly what you'd want it to. I came across a similar problem, and my solution was to have a method which hides the navigation bar, which is called after a delay of 0 seconds as follows.

The method that is called is:

-(void) hideNavBar {
    if (self.navigationController.navigationBar.hidden == NO)
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

Then in the viewDidLoad method, I have the following:

[self performSelector:@selector(hideNavBar) withObject:nil afterDelay:0.0];

This works and removes the navigation bar in one instantaneous swoop. You can amend the delay time if you want the animation or for it to be removed after a delay. I tried [self hideNavBar] but that simply did not work, so sticking to what I have above.

Hope this helps, and if someone has a more elegant solution, I'm interested!