2

Is there a way to change (namely, decrease) the height of a UINavigationBar in a UINavigationController? I have tried the following to no avail:

extension UINavigationBar {

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

}

class CustomNavigationController : UINavigationController {

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        navigationBar.frame.size.height = 10
    }

}

Thanks in advance!

Sergio Charles
  • 329
  • 3
  • 13

1 Answers1

1

Try the below codes,

override func viewDidLayoutSubviews()
{
   super.viewDidLayoutSubviews()
   let height = CGFloat(72)
   navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
}
Angel F Syrus
  • 1,607
  • 7
  • 18
  • 36
  • Thanks! I have a question: UINavigationControllers have `viewWillLayoutSubviews()`, but not `viewDidLayoutSubviews()` so where should I implement the above block of code? Or should I use `viewDidAppear`? – Sergio Charles Aug 31 '19 at 07:42
  • Is this what you are referring to?: https://stackoverflow.com/questions/40751366/how-can-i-change-height-of-navigation-bar-swift-3?rq=1 – Sergio Charles Aug 31 '19 at 07:50
  • No you have to write the code inside the UINavigationController's class – Angel F Syrus Aug 31 '19 at 08:33
  • I think viewDidLayoutSubviews() was deprecated in iOS 11 (see [this](https://stackoverflow.com/questions/39780470/viewdidlayoutsubviews-no-longer-called-when-popping-top-view-with-uinavigationco)) – Sergio Charles Aug 31 '19 at 18:13