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

rad*_*tiv 81 uinavigationbar ios swift ios11

我让我UISearchBar成为导航栏的一部分,如:

 let searchBar = UISearchBar()
 //some more configuration to the search bar
 .....
 navigationItem.titleView = searchBar
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

现在iOS 11我有:

在此输入图像描述

正如你所看到的那样,两个搜索栏的舍入存在差异,这并不困扰我.问题是搜索栏会增加导航栏的高度.所以,当我去另一个控制器时,它看起来很奇怪:

在此输入图像描述

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

任何想法如何摆脱黑线和所有视图控制器具有一致的导航栏高度?

zgj*_*jie 65

您可以将高度为44的约束添加到iOS 11的搜索栏中.

if #available(iOS 11.0, *) {
    searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
Run Code Online (Sandbox Code Playgroud)

// Objective-c

   if (@available(iOS 11.0, *)) {
      [searchBar.heightAnchor constraintEqualToConstant:44].active = YES;
    }
Run Code Online (Sandbox Code Playgroud)

  • 这还不够.单击该字段时,大小仍然混乱. (14认同)
  • 虽然这可以使高度恢复正常,但如果我点击我的搜索栏,由于某种原因它会切断搜索栏的底部.任何想法为什么会发生这种情况或其他人有这个问题? (11认同)
  • 完全奇怪的是你是否使用这个代码,因为它只是缩小了导航栏,但搜索栏的高度仍然是56. (9认同)
  • 这在 iOS 13 中不起作用。有解决方法吗? (6认同)
  • 有没有其他人找到解决方案?这样做会在搜索栏中产生许多奇怪的行为问题. (4认同)
  • 这是obj-c版本:[searchBar.heightAnchor constraintEqualToConstant:44] .active = YES; 但有一个问题是,虽然这解决了高度问题,但它对UISearchController的其余视图造成了严重破坏.例如,条形按钮首先被隐藏,放大镜看起来是半绘制的,或者好像有什么东西在它上面.还有谁? (3认同)
  • 我非常肯定在iOS 11.4中它不再起作用了.Apple在搜索栏上设置了严格的高度限制.如果你通过设置`NSAutoresizingMaskLayoutConstraint = false`来打破它,Apple只会强制执行`_UITemporaryLayoutHeight`来覆盖你. (3认同)

And*_*rew 57

在两种情况下,我在iOS5的NavigationBar下使用SearchBar获得黑线:

  • 当我使用UISearchBar从ViewController推送另一个ViewControllers时 在此输入图像描述

  • 当我用UISearchBar解雇ViewController时"向右拖动以解除" 在此输入图像描述

我的解决方案是:使用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
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

  • 请谨慎使用此选项,因为对我来说,当尝试滑回到上一个视图控制器时,它会使整个应用程序冻结 (2认同)
  • 这个解决方案确实让我松了一口气,谢谢@andrew (2认同)

Mai*_*Mai 37

我相信在iOS 11中,UISearchBar现在的高度等于56,而UINavigationBar使用autolayout来适应其子视图,因此它增加了高度.如果您仍然希望UISearchBar与iOS 11之前的titleView一样,我发现最好的方法是在自定义视图中嵌入UISearchBar,并将此视图的高度设置为44,并将其分配给navigationItem.titleView

class SearchBarContainerView: UIView {  

    let searchBar: UISearchBar  

    init(customSearchBar: UISearchBar) {  
        searchBar = customSearchBar  
        super.init(frame: CGRect.zero)  

        addSubview(searchBar)  
    }

    override convenience init(frame: CGRect) {  
        self.init(customSearchBar: UISearchBar())  
        self.frame = frame  
    }  

    required init?(coder aDecoder: NSCoder) {  
        fatalError("init(coder:) has not been implemented")  
    }  

    override func layoutSubviews() {  
        super.layoutSubviews()  
        searchBar.frame = bounds  
    }  
}  

class MyViewController: UIViewController {  

    func setupNavigationBar() {  
        let searchBar = UISearchBar()  
        let searchBarContainer = SearchBarContainerView(customSearchBar: searchBar)  
        searchBarContainer.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 44)  
        navigationItem.titleView = searchBarContainer  
    }  
} 
Run Code Online (Sandbox Code Playgroud)


小智 16

在viewDidLoad中的"ACKNOWLEDGMENTS"视图控制器上尝试此代码

self.extendedLayoutIncludesOpaqueBars = true
Run Code Online (Sandbox Code Playgroud)


Ble*_*Neo 7

谢谢你们!我终于找到了解决方案。

使用 UISearchBar 将以下代码添加到 ViewController。

  1. 第一步: viewDidLoad
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    ...
}
Run Code Online (Sandbox Code Playgroud)
override func viewDidLoad() {
    super.viewDidLoad()
    self.extendedLayoutIncludesOpaqueBars = true
}
Run Code Online (Sandbox Code Playgroud)
  1. 第二步:viewWillDisappear
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     // force update layout
    [self.navigationController.view setNeedsLayout]; 
    // to fix height of the navigation bar
    [self.navigationController.view layoutIfNeeded];  
}
Run Code Online (Sandbox Code Playgroud)
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.view.setNeedsLayout() // force update layout
        navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
    }
Run Code Online (Sandbox Code Playgroud)