gpt4 book ai didi

ios - UISearchBar 增加了 iOS 11 中的导航栏高度

转载 作者:IT王子 更新时间:2023-10-29 04:57:21 26 4
gpt4 key购买 nike

我的 UISearchBar 是导航栏的一部分,例如:

 let searchBar = UISearchBar()
//some more configuration to the search bar
.....
navigationItem.titleView = searchBar

更新到 iOS 11 后,我的应用程序中的搜索栏发生了一些奇怪的事情。在 iOS 10 和之前我的导航栏看起来像:

enter image description here

现在 iOS 11 我有:

enter image description here

如您所见,两个搜索栏的四舍五入有所不同,但我并不介意。问题是搜索栏增加了导航栏的高度。所以当我转到另一个 Controller 时,它看起来也很奇怪:

enter image description here

事实上,奇怪的黑线的高度加上当前导航栏的高度等于第二张图片中导航栏的高度......

有什么想法可以消除黑线并在所有 View Controller 中保持一致的导航栏高度吗?

最佳答案

在两种情况下,我在 iOS 11 的 NavigationBar 和 SearchBar 下出现黑线:

  • 当我使用 UISearchBar 从 ViewController 推送另一个 ViewController 时 enter image description here

  • 当我使用 UISearchBar 关闭 ViewController 时使用“拖动权利关闭” enter image description here

我的解决方案是:使用 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/

26 4 0