UISplitViewController with new UISearchController issue with UISearchBar

∥☆過路亽.° 提交于 2020-01-07 04:01:50

问题


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



来源:https://stackoverflow.com/questions/27189189/uisplitviewcontroller-with-new-uisearchcontroller-issue-with-uisearchbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!