UISplitViewController与UISearchBar的新UISearchController问题 [英] UISplitViewController with new UISearchController issue with UISearchBar
问题描述
我最近开始重写我的iOS应用,以使用新的UISearchController
和通用情节提要.
我的应用程序可用于两种设备( iPhone 和 iPad ),因此使用UISplitViewController
更改为通用情节提要是一个很大的优势.
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.
但是可悲的是,UISearchController
无法正常工作.我用以下几行添加了UISearchController
:
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
问题在于,在iPad应用程序中,UISearchBar
被UINavigationBar
覆盖.但是,如果我切换选项卡并返回视图. UISearchBar
可见.因此,以某种方式在标签栏切换后,它可以正确地重绘视图.在iPhone版本中,它会自动正确运行.
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应用
第一次启动后,UISearchBar
被UINavigationBar
切换选项卡后,UISearchBar
正确显示
After switching the tabs the UISearchBar
is displayed correctly
iPhone应用
iPhone应用程序可以正常运行,而无需更改选项卡.
The iPhone app is working correctly without changing the tabs..
我尝试过的事情:
- 使用extendedEdges的不同设置
- 在viewWillAppear方法中添加SearchController,因为我认为以后可以通过添加搜索控件来欺骗它
推荐答案
我假设您在viewDidload中设置了搜索控制器self.searchController.searchBar.sizeToFit()
.除此之外,您需要添加此方法(Objective-C代码):
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];
}
如果它不起作用,则可以将以下代码添加到viewDidlLoad
或viewDidLayoutSubviews
:
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
这篇关于UISplitViewController与UISearchBar的新UISearchController问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!