How to hide UISearchController from navigation item?

In iOS 11.0+ I use the following statements to add a UISearchController to the navigation bar:


[[self navigationItem] setSearchController:[self myUISearchController]];
[[self navigationItem] setHidesSearchBarWhenScrolling:NO];
[self setDefinesPresentationContext:YES];


To hide the UISearchController I tried the following:

[[self myUISearchController] setActive:NO];
[[self myUISearchController] removeFromParentViewController]; // just a try
[[self navigationItem] setSearchController:nil];              // this should be sufficient
[self myUISearchController:nil];


Actually, the search controller disappears but leaves a black rectangle at the position where it was. It seems to be that the UITableViewController inside the UINavigationController does not re-align its table view and therefore leaves a black rectangle.


Any ideas?

Replies

Was facing the same issue.

Calling setNeedsLayout() on the UINavigationController's view after setting the search controller to nil helped in my case.

  • This is the only solution I could find to reliably show/hide the UISearchController instance from the navigation bar as well as clearing its space when hidden. However, I wasn't able to show/hide it with animation. I'm not sure if navigationItem.searchController is animatable.

Add a Comment