我正在使用新的 iOS 11 searchContoller
UINavigationItem
的属性。我正在运行 iOS 11.0 GM 版本。
当我在搜索 Controller 处于事件状态时执行推送转场时,它工作正常。当我随后弹出时,搜索栏折叠并在状态栏中被压扁。我无法取消搜索,也无法编辑搜索文本。
请参阅以下图像序列:
最终图像显示了在 pop segue 期间表格的出现,以从呈现的 View Controller 返回到带有搜索栏的表格。奇怪的是,这并不总是发生。它发生在 90% 的时间,但有时它表现良好。我还没有弄清楚我正在做些什么来让它发挥作用。搜索栏被压扁后,我必须强制关闭应用程序才能恢复到正常状态。
设置搜索 Controller 的代码非常标准。 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)
}
我还在 Apple 的消息应用程序(请参见下面的屏幕截图)以及“设置”、“便笺”和“邮件”中注意到了这个问题,因此推测这是一个 iOS 11 错误。
这似乎只有在设置 -> 常规 -> 辅助功能 -> 较大文本中使用小于默认文本大小时才会发生,并且似乎只发生在物理设备上(尚未在模拟器中重现)。在viewDidAppear
中,searchController.searchBar.frame.height
等于0
(但在viewDidDisappear
中不是,不是viewWillAppear
)。到目前为止,我唯一的解决方法是:
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 11.0, *), searchController.searchBar.frame.height == 0 {
navigationItem.searchController?.isActive = false
}
super.viewDidAppear(animated)
}
有没有更好的方法来解决这个问题?
最佳答案
此错误可在 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;
}
}
避免在 VC 生命周期事件上改变 navigationItem,解决了我的问题
关于ios - 在 iOS 11 上,导航项中的搜索栏在导航弹出时折叠并卡在状态栏下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46239530/