Change UINavigationBar Height

前端 未结 12 1526
囚心锁ツ
囚心锁ツ 2020-11-28 09:26

Can some one tell me how to change the navigation bar height?

Here is what i have so far:

CGFloat navBarHeight = 10;
self.navigationController.navi         


        
12条回答
  •  庸人自扰
    2020-11-28 09:58

    Creating category and overriding sizeThatFits(_ size: CGSize) -> CGSize method changes UINavigationBar size all over the app. Assuming you want to change UINavigationBar height/size only in one particular ViewController (in my case)

    This is how I did it.

    extension UINavigationBar {
        open override func sizeThatFits(_ size: CGSize) -> CGSize {
            let v = self.value(forKey: "frame") as? CGRect
            return v?.size ?? CGSize(width: UIScreen.main.bounds.width, height: 44)
        }
    }
    

    in caller ViewController would look like

    override func viewDidLoad() {
        super.viewDidLoad()
        configureNavigationBar()
    }
    
    private func configureNavigationBar() {
        var naviBarFrame = self.navigationController?.navigationBar.frame
        naviBarFrame?.size.height = 74
        let rect = naviBarFrame ?? CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: 74)
        self.navigationController?.navigationBar.setValue(rect, forKey: "frame")
    }
    

提交回复
热议问题