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?

Was facing the same issue.

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

How to hide UISearchController from navigation item?
 
 
Q