iOS11 customize navigation bar height

前端 未结 2 857
温柔的废话
温柔的废话 2020-12-28 18:53

First of all, thank you for coming here and help solving my problem. Thank you!!!

In iOS11 beta6, sizeThatFits: seems to not work on UINavigationBar. I

相关标签:
2条回答
  • 2020-12-28 19:20

    Since iOS 11 UINavigationBar fully supports Auto Layout (this is the reason why you're seeing its constraints). I've opened a radar to Apple because I thought that setting a height constraint to the titleView would have adjusted the navigation bar height accordingly. However this is what Apple replied:

    Full support for auto layout does not imply that your view can influence other aspects of the layout of the navigation bar – in particular, the navigation bar enforces its own height and does not allow the title view or other custom views to exceed the height of the navigation bar. We are continuing to work on this issue, and will follow up with you again.

    As of today the radar is still open.

    0 讨论(0)
  • 2020-12-28 19:37

    Hello I just experienced this same issue.

    Now the top layout guide is deprecated on iOS 11. You have to reference the safeAreaLayoutGuide in your constraints.

    Here's an example in swift

        if #available(iOS 11, *) {
            let guide = self.view.safeAreaLayoutGuide.topAnchor
            let height = (self.navigationController?.navigationBar.frame.height)! - CGFloat(12)
            NSLayoutConstraint.activate([
            self.yourTableView.topAnchor.constraint(equalTo: guide, constant: height)
            ])
        }
    

    As you can see your view's top anchor should match the safeAreaLayoutGuide top anchor. In this example I'm using the variable height to create the new constraint. The variable height contains the navigation bar height minus a constant.

    You should try by changing the height value.

    Hope this helps you.

    0 讨论(0)
提交回复
热议问题