Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

ios - UISplitViewController with new UISearchController issue with UISearchBar

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 the first launch the UISearchBar is covered by the UINavigationBar

After switching the tabs the UISearchBar is displayed correctly

After switching the tabs the UISearchBar is displayed correctly

iPhone App

The iPhone app is working correctly without changing the tabs.. iPhone App works as expected

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...