Programmatically change height of navigation bar in swift

前端 未结 2 856
太阳男子
太阳男子 2020-12-19 22:06

I want to make a taller navigation bar and I am trying to do this using a subclass of UINavigationBar.

Here is my subclass of UINavigationBar:

import         


        
2条回答
  •  悲哀的现实
    2020-12-19 22:42

    Here is a simple Swift 3 solution based on minimal subclassing. First subclass UINavigationBar.:

    class CustomNavigationBar: UINavigationBar {
        override func sizeThatFits(_ size: CGSize) -> CGSize {
            let newSize :CGSize = CGSize(width: self.frame.size.width, height: 54)
            return newSize
        }
    }
    

    Create the navigation controller in code and use the initializer that takes your custom navigation bar class.

    let nav = UINavigationController(navigationBarClass: CustomNavigationBar.self, 
                                     toolbarClass: nil)
    

    All existing behavior for UINavigationBar is preserved and your custom height is adopted.

提交回复
热议问题