UISearchController search bar misplaced when UISplitViewController mode is all visible

659 views Asked by At

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):

enter image description here

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:

enter image description here

When split view controller is in UISplitViewControllerDisplayModePrimaryHidden mode, that won't happen, search bar is displayed completely normal:

enter image description here

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;
        });
    }
}
1

There are 1 answers

0
Brennan On

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:

self.definesPresentationContext = true

source: searchBar from UISearchController not showing correctly on split view on iPad