UISplitViewController with new UISearchController

2019-08-22 01:26发布

问题:

I recently started to rewrite my iOS app to use the new UISearchController and a universal storyboard. My app is available for both devices (iPhone and iPad) so the change to the universal storyboard using the UISplitViewController was a big advantage.

But sadly the UISearchController isn't working as expected. I added the UISearchController with the following lines:

self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchBar.sizeToFit()
self.searchController.dimsBackgroundDuringPresentation = false
self.myTableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.definesPresentationContext = true

My controller chain is like that:

UISplitViewController
    UITabbarController (Master)
        UINavigationController
            UITableViewController
    UINavigationController (Detail)
        UINavigationController
            UIViewController

The problem is that in the iPad app the UISearchBar is covered by the UINavigationBar. But if I switch the tabs and go back to the view. The UISearchBar is visible. So somehow after a tabbar switch it redraws the view correctly. In the iPhone Version it works automatically correct.

iPad App

After the first launch the UISearchBar is covered by the UINavigationBar

After switching the tabs the UISearchBar is displayed correctly

iPhone App

The iPhone app is working correctly without changing the tabs..

What I tried:

  • Using different settings of extendingEdges
  • Add the SearchController in the viewWillAppear method because I thought I can trick it by adding the search control later on

回答1:

I assume you setup search controller self.searchController.searchBar.sizeToFit() in viewDidload. Beside that, you need to add this method (Objective-C code):

- (void)viewDidLayoutSubviews {
    // you could check
    // if (IS_IPAD || (IS_IPHONE_6PLUS && UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))) 
    [self.searchController.searchBar sizeToFit];
}

In case it does not work, you could add these codes into your viewDidlLoad or viewDidLayoutSubviews:

self.edgesForExtendedLayout = UIRectEdgeNone;
self.yourTableViewController.edgesForExtendedLayout = UIRectEdgeNone;

If you wants to dock your search, please refer my answer here