3

Controller presents with the following code:

let vc = UIViewController()
vc.view.backgroundColor = Colors.green
let nvc = UINavigationController(rootViewController: vc)
self.present(nvc, animated: true, completion: nil)

It shows

original

but the navigation bar height is big. It should be like this

shouldbe

How to do the navigation bar height like on the second image?

2 Answers2

3

From the discussion in apple developer forum, this seems to be a bug in iOS 13.
And the workaround they provide there is as following:

override func viewWillAppear(_ animated: Bool) {  
    super.viewWillAppear(animated)  
    if #available(iOS 13.0, *) {  
        navigationController?.navigationBar.setNeedsLayout()  
    }
}

I've tested it in my project and it solved the problem. Hope it solves yours too.

inexcii
  • 1,591
  • 2
  • 15
  • 28
2

The navigation bar is actually in the right size (at least for iOS 13 standards) when the stack is not too deep. The navigation bar becomes smaller for a view presented at index 2 (and later?)

To resize the navigation bar, follow these two steps:

  1. Get the default height constraint (with name "UI-View-Encapsulated-Layout-Height"). To do this, search among the navigation bar's constraints. I've shamelessly copied Isaih Turner's solution below, which makes it easier.

    extension UIView {
        /// Returns the first constraint with the given identifier, if available.
        ///
        /// - Parameter identifier: The constraint identifier.
        func constraintWithIdentifier(_ identifier: String) -> NSLayoutConstraint? {
            return self.constraints.first { $0.identifier == identifier }
        }
    }
    
  2. Set the constant to the desired value.

    let existingConstraint = self.navigationController?.navigationBar.constraintWithIdentifier("UIView-Encapsulated-Layout-Height")
    existingConstraint?.constant = 88.0
    self.navigationController?.navigationBar.setNeedsLayout()
    

And you're done.

Nick Toumpelis
  • 2,717
  • 22
  • 38