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

标签 ios swift uinavigationbar ios11

我的 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/

相关文章:

c# - iOS:流式传输大文件

ios - 如何在 userDefaults swift 中进行循环

swift - 向下滑动弹出 OS X Swift

iOS 在整个应用程序中更改 NavigationBar 外观

ios - 如何在 Xcode 的 UITests 下等待,直到某些 View 可见以供点击?

ios - UITableViewCell 的自定义行动画

ios - 在 swift 中渲染 UIImageView 时质量松散

ios - 以编程方式向 UITableView 添加导航栏

ios - 双导航栏

ios - 由于 '_alwaysRunsAtForegroundPriority',Ionic 应用程序在 iOS 12.2 上崩溃