iOS 11 Beta中的scopeButtons忽略了UISearchController.hidesNavigationBarDuringPresentation [英] UISearchController.hidesNavigationBarDuringPresentation ignored with scopeButtons in iOS 11 Beta
问题描述
在我们的项目中,我们指定了
hidesNavigationBarDuringPresentation = false
特定UIViewController
的UISearchController
上的
. searchController具有一系列作用域标题.到目前为止,它在iOS 10上都可以正常工作,但是在iOS 11 Beta中,似乎hidesNavigationBarDuringPresentation的错误设置被忽略,使我们的显示混乱.为了确保不是我项目中的其他因素,我创建了一个仅有一个UITableViewController
的准系统测试项目,并使用另一个简单的UITableViewController
初始化了UISearchController
.以下代码位于主视图控制器上的viewDidLoad()
方法中:
self.title = "Search Bar Scope Test"
let searchViewController = SearchViewController(style: .plain)
searchController = UISearchController(searchResultsController: searchViewController)
searchController!.searchBar.sizeToFit()
tableView.tableHeaderView = searchController!.searchBar
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.scopeButtonTitles = ["scope 1", "scope 2", "scope 3", "scope 4", "scope 5"]
当不存在分配scopeButtonTitles
的最后一行时,导航栏没有隐藏,搜索栏保持在其原始位置.但是,在显示该行的情况下,NavigationBar
变为隐藏状态,并且searchBar
以及合并范围按钮在iPhone和iPad上均以纵向模式向上移动,但在横向模式下保持不变(即使合并范围按钮很多且不能排成一行).
还有其他人遇到吗?这是iOS 11中的错误或预期行为(当然不是我们的预期行为),并且有任何解决方法吗?
谢谢!
好,在研究另一个相关问题时,发现了问题的原因,原因是searchBar
和范围按钮在iOS 11中未对齐.关键是iOS 11中的searchController配置方案已更改,其中searchBar
不再显示为tableView's
headerView
,相反,整个searchController应该是navigationItem
的一部分,如下所示:
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
// optional, but apparently due to a bug in iOS 11,
// the searchBar and the scope buttons may get too high and mis-aligned
// when the nav bar is hidden
searchController?.hidesNavigationBarDuringPresentation = false
} else {
tableView.tableHeaderView = searchController!.searchBar
}
上面的代码修复了我与iOS 11中的UISearchBar相关的一些UI问题,实际上是在此WWDC 2017视频,但是我希望Xcode能够在旧的tableHeaderView分配行上给我们警告,这可能节省了我,并且可能省去了很多其他开发人员的困惑和研究时间./p>
In our project we specified that
hidesNavigationBarDuringPresentation = false
on a particular UIViewController
's UISearchController
. The searchController has an array of scope titles. This so far works fine up to iOS 10, but in iOS 11 betas, it looks like the false setting of hidesNavigationBarDuringPresentation is ignored and messes up our display. To make sure it's not because of other factors in my project, I created a bare-bone test project with just a single UITableViewController
, with a UISearchController
initialized with another simple UITableViewController
. The following code is in the viewDidLoad()
method on the main view controller:
self.title = "Search Bar Scope Test"
let searchViewController = SearchViewController(style: .plain)
searchController = UISearchController(searchResultsController: searchViewController)
searchController!.searchBar.sizeToFit()
tableView.tableHeaderView = searchController!.searchBar
searchController?.hidesNavigationBarDuringPresentation = false
searchController?.searchBar.scopeButtonTitles = ["scope 1", "scope 2", "scope 3", "scope 4", "scope 5"]
When the last line assigning scopeButtonTitles
is not present, the navigation bar didn't get hidden and the search bar remains in its original position. However, with that line present, the NavigationBar
becomes hidden and the searchBar
plus the scope buttons all moved up in portrait mode on both iPhone and iPad, but stays the same in landscape mode (even if the scope buttons are many and can't fit in one line).
Did anybody else encounter this? Is this a bug or expected behavior (certainly not our desired behavior though) in iOS 11, and is there any workaround?
Thanks!
OK, found the cause of the problem while I was researching another related issue, that the searchBar
and the scope button got mis-aligned in iOS 11. The key is that the searchController configuration scheme has changed in iOS 11 where searchBar
should no longer be presented as the tableView's
headerView
, instead, the whole searchController should be part of the navigationItem
, as illustrated below:
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
// optional, but apparently due to a bug in iOS 11,
// the searchBar and the scope buttons may get too high and mis-aligned
// when the nav bar is hidden
searchController?.hidesNavigationBarDuringPresentation = false
} else {
tableView.tableHeaderView = searchController!.searchBar
}
The above code fixed a few UI problems I had related to UISearchBar in iOS 11, and is actually recommended in this WWDC 2017 video, but how I wish if Xcode could give us a warning at the old tableHeaderView assignment line, it would have saved me and probably quite some other developers' confusion and research time.
这篇关于iOS 11 Beta中的scopeButtons忽略了UISearchController.hidesNavigationBarDuringPresentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!