我的 UISearchBar
是导航栏的一部分,例如:
let searchBar = UISearchBar()
//some more configuration to the search bar
.....
navigationItem.titleView = searchBar
更新到 iOS 11
后,我的应用程序中的搜索栏发生了一些奇怪的事情。在 iOS 10
和之前我的导航栏看起来像:
现在 iOS 11
我有:
如您所见,两个搜索栏的四舍五入有所不同,但我并不介意。问题是搜索栏增加了导航栏的高度。所以当我转到另一个 Controller 时,它看起来也很奇怪:
事实上,奇怪的黑线的高度加上当前导航栏的高度等于第二张图片中导航栏的高度......
有什么想法可以消除黑线并在所有 View Controller 中保持一致的导航栏高度吗?
最佳答案
在两种情况下,我在 iOS 11 的 NavigationBar 和 SearchBar 下出现黑线:
我的解决方案是:使用 UISearchBar 将这段代码添加到我的 ViewController 中:
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController.view setNeedsLayout]; // force update layout
[self.navigationController.view layoutIfNeeded]; // to fix height of the navigation bar
}
Swift 4 更新
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.view.setNeedsLayout() // force update layout
navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
}
关于ios - UISearchBar 增加了 iOS 11 中的导航栏高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46318022/