UISplitViewController与新的UISearchController问题与UISearchBar

我最近开始重写我的iOS应用程序,使用新的UISearchController和一个通用的故事板。 我的应用程序适用于这两种设备( iPhoneiPad ),所以使用UISplitViewController更改为通用故事板是一大优势。

但不幸的是, UISearchController没有按预期工作。 我用下面几行添加了UISearchController

 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 

我的控制器链是这样的:

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

问题是在UISearchBar覆盖的iPad应用程序。 但是,如果我切换选项卡,并返回到视图。 UISearchBar是可见的。 所以不知何故,在一个tabbar开关后,它正确地重绘视图。 在iPhone版本中,它会自动更正。

iPad应用程序

第一次启动后, UISearchBarUINavigationBar覆盖

第一次启动后,UISearchBar被UINavigationBar覆盖

切换标签后, UISearchBar显示正确

切换标签后,UISearchBar显示正确

iPhone应用程序

iPhone应用程序正常工作,无需更改标签。 iPhone应用程序按预期工作

我试过的:

  • 使用extendsEdges的不同设置
  • 在viewWillAppear方法中添加SearchController,因为我以后可以通过添加search控件来欺骗它

我假设你在viewDidload中设置了search控制器self.searchController.searchBar.sizeToFit() 。 除此之外,你需要添加这个方法(Objective-C代码):

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

如果它不起作用,你可以添加这些代码到你的viewDidlLoadviewDidLayoutSubviews

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

如果你想停靠你的search,请参考我的答案