How can I change height of Navigation Bar - Swift 3

 ̄綄美尐妖づ 提交于 2019-12-27 17:37:29

问题


I'm pretty sure this is not duplicate because I looked at other answers and they all outdated and do not provide exact answer.

I have Navigation Controller and several view controllers. I want to make Navigation Bar a bit taller so it would fit text size that I need. How can I do that ?

I tried this:

UINavigationBar.appearance().frame = CGRect(x: 0.0, y: 0.0, width: 320.0, height: 210.0) 

but nothing happens...

Also, I wasn't able to add any constraints to the Nav Bar using Xcode layout buttons.

Hope somebody can help me to fix this issue. Thanks in advance!


回答1:


Here is one way to do it:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let height: CGFloat = 50 //whatever height you want to add to the existing height
    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)

}



回答2:


You can write this code in class that extends UINavigationController

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let height = CGFloat(72)
    navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
}



回答3:


You can create a class based on UINavigationBar like this

class TTNavigationBar: UINavigationBar {

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width, height: 55)
    }

}

And add it to your NavigationController.




回答4:


This might be better if your navigation bars in your app all have same adjusted height.

extension UINavigationBar {
    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width, height: 51)
    }
}



回答5:


you can draw your navigation bar from xib (with your height) and then:

self.navigationController?.navigationBar.addSubview(yourNavBarView)

in alternative, if you want create UIView from code,

let viewNavBar = UIView(frame: CGRect(
    origin: CGPoint(x: 0, y:0), 
    size: CGSize(width: self.view.frame.size.width, height: 100)))

and then adding to nav bar.



来源:https://stackoverflow.com/questions/40751366/how-can-i-change-height-of-navigation-bar-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!