My root view controller is a UISplitViewController
with preferredDisplayMode
is UISplitViewControllerDisplayModeAllVisible
. In detail view controller, I have a UISearchController
, but I don't have a UITableView
to attach the search bar, so I placed a UIView
inside the view and add search bar into it:
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.hidesNavigationBarDuringPresentation = NO;
// This is the view to contain search bar
_searchView.backgroundColor = [UIColor greenColor];
[_searchView addSubview:_searchController.searchBar];
self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
UPDATE: This also happened with UISearchBar
set to tableView.tableHeaderView
, not only when contained in a UIView
.
This is what it looks like at first (I cropped the height of the image):
But when the search controller is active, the search bar frame's origin.x
is misplaced to the right, exactly equals to width of master view controller:
When split view controller is in UISplitViewControllerDisplayModePrimaryHidden
mode, that won't happen, search bar is displayed completely normal:
So is that a bug of UISplitViewController
? Currently I fix that with this ugly way, reset origin.x
of search bar, it's kinda glitchy:
- (void)didPresentSearchController:(UISearchController *)searchController {
searchController.searchBar.superview.clipsToBounds = NO;
if (self.splitViewController.displayMode == UISplitViewControllerDisplayModeAllVisible) {
searchController.searchBar.frame = ({
CGRect frame = searchController.searchBar.frame;
frame.origin.x -= self.splitViewController.primaryColumnWidth;
frame;
});
}
}
This was giving me tons of trouble. My app worked on everything but iPads because of the search bar being misplaced in the SplitView. I finally came across someone who posted the solution. Add this to the end of viewDidLoad() for your table view:
source: searchBar from UISearchController not showing correctly on split view on iPad