[英]Search Bar in a Navigation Item collapses and gets stuck under status bar upon navigation pop, on iOS 11
I am using the new iOS 11 searchContoller
property of a UINavigationItem
. 我正在使用UINavigationItem
的新iOS 11 searchContoller
属性。 I am running iOS 11.0 GM build. 我正在运行iOS 11.0 GM build。
When I perform a push segue whilst the search controller is active, it works fine. 当我在搜索控制器处于活动状态时执行push segue时,它可以正常工作。 When I subsequently pop back, the search bar is collapsed and squashed up in the status bar. 当我随后弹回时,搜索栏将折叠并在状态栏中压缩。 I cannot then cancel the search, or edit the search text. 我无法取消搜索,或编辑搜索文本。
See the following sequence of images: 请参阅以下图像序列:
The final image is showing the appearing of the table during the pop segue to return from a presented view controller back to the table with the search bar. 最终图像显示在pop segue期间表格的出现,以便从呈现的视图控制器返回到具有搜索栏的表格。 Strangely, this doesn't always happen. 奇怪的是,这并不总是发生。 It happens 90% of the time, but sometimes it behaves fine. 它发生在90%的时间,但有时它表现得很好。 I haven't yet worked out what I am doing differently to make it work. 我还没有弄清楚我在做什么,以使其发挥作用。 Once the search bar is squashed, I have to force close the app to get back to a sensible state. 一旦搜索栏被压扁,我必须强制关闭应用程序以恢复合理的状态。
The code which sets up the search controller is pretty standard. 设置搜索控制器的代码非常标准。 The relevant bit of viewDidLoad()
is as follows: viewDidLoad()
的相关位如下:
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.returnKeyType = .done
searchController.searchBar.placeholder = "Your Library"
searchController.searchBar.searchBarStyle = .minimal
// We will manage the clearing of selections ourselves.
clearsSelectionOnViewWillAppear = false
// Some search bar styles are slightly different on iOS 11
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
navigationController!.navigationBar.prefersLargeTitles = true
}
else {
searchController.searchBar.backgroundColor = tableView.backgroundColor!
searchController.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.height), animated: false)
}
I've also noticed this issue in Apple's Messages app (see screenshot below), along with Settings, Notes and Mail, so presumably this is an iOS 11 bug. 我也注意到Apple的Messages应用程序中的这个问题(见下面的截图),以及Settings,Notes和Mail,所以这可能是iOS 11的错误。
This only seems to happen when using smaller than default Text Size in Settings -> General -> Accessibility -> Larger Text, and only seems to happen on a physical device (haven't reproduced it in the simulator). 这仅在设置 - >常规 - >辅助功能 - >较大文本中使用小于默认文本大小时才会发生,并且似乎只发生在物理设备上(尚未在模拟器中复制)。 In viewDidAppear
, searchController.searchBar.frame.height
is equal to 0
(but not in viewDidDisappear
, not viewWillAppear
). 在viewDidAppear
, searchController.searchBar.frame.height
等于0
(但不在viewDidDisappear
,而不是viewWillAppear
)。 The only workaround I have so far is: 到目前为止我唯一的解决方法是:
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 11.0, *), searchController.searchBar.frame.height == 0 {
navigationItem.searchController?.isActive = false
}
super.viewDidAppear(animated)
}
Is there a better way to get around this problem? 有没有更好的方法来解决这个问题?
This bug can be reproduced in iOS 11.1: 这个bug可以在iOS 11.1中重现:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesSearchBarWhenScrolling = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (@available(iOS 11.0, *)) {
self.navigationItem.hidesSearchBarWhenScrolling = YES;
}
}
Avoiding mutate navigationItem on VC lifecycle events, fixed problem for me 在VC生命周期事件上避免mutate navigationItem,修复了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.