I have implemented the UISearchController and it is working great except...
When I click on the Search Bar the Navigation Bar disappears nicely as expected. When I rotate the phone to landscape view I get this view which makes sense.
However, when I rotate the phone back to portrait view (still selected in search type area) I get this following view.
You can see that the Navigation Bar never reappears. I feel I'm implementing a basic search controller. What could possibly be causing this?
self.venueSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.venueSearchController.searchResultsUpdater = self;
self.venueSearchController.searchBar.delegate = self;
self.venueSearchController.dimsBackgroundDuringPresentation = NO;
self.venueSearchController.hidesNavigationBarDuringPresentation = YES;
self.venueSearchController.searchBar.frame = CGRectMake(self.venueSearchController.searchBar.frame.origin.x, self.venueSearchController.searchBar.frame.origin.y, self.venueSearchController.searchBar.frame.size.width, 44.0);
self.definesPresentationContext = YES;
self.navigationController.navigationBar.translucent = YES;
self.venueSearchController.searchBar.translucent = YES;
self.tableView.tableHeaderView = self.venueSearchController.searchBar;
It looks like the
UISearchController
forgets to reset the frame of thesearchBar
when the status bar reappears. I think this is probably a bug inUISearchController
; there seem to be a few listed in radar. It seems the searchBar's superview (which is internal to the UISearchController) ends up with the wrong height. This is vexing since the solution therefore involves reaching into the searchController's view hierarchy, which Apple could change... you might want to add a check of the iOS version so it only runs for specified versions.If you add the code below to your view controller, it will be called when the trait collection changes. It checks to see a) the search controller is active, b) the statusBar is not hidden, and c) the searchBar origin-y is 0, and if so it increases the height of the superview by the height of the statusBar, which moves the searchBar down.
Objective C
You may need to implement
UIBarPositioningDelegate
in your view Controller.The searchBar is looking for a response from it's delegate.
Add the following to your
self
(I assume it's a ViewController)I've faced a similar issue with
UISearchController
using it withnavigationItem
. It looks differently, but caused by the very same problem:UISearchController
does NOT updatesearchBar
frame when status bar reappears.But instead of manually adjusting frames of
searchBar
's view hierarchy you should simply force it to update layout by togglinghidesNavigationBarDuringPresentation
back and forth during rotation either intraitCollectionDidChange
orviewWillTransition(to size: CGSize, with cooridnator: UIViewControllerTransitionCoordinator)
:So the fix would look like this:
And here is how the original issue looks like.
Then rotating and got this:
I've "fixed" this temporarily by turning off "Hide status bar" in settings.
I believe this may be an issue with the way UITableViewController layout deals with the navigation bar going as opposed to there being no navigation bar when it appears in the case of you rotating back.
I think you can solve this issue by replacing your UITableViewController with a UIViewController into which you drop a UITableView. Then set the top constraint of the table view to be the top layout guide. Set the side constraints to left, right, bottom of 0.
This should ensure that the table always stays below the status bar and will still move correctly when the nav bar goes and comes back.
See this discussion: iOS 7: UITableView shows under status bar