Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...