0

I am facing this situation where the navigation bar looks OK in portrait mode but gets cropped in landscape:

portrait portraid mode

landscape landscape mode

I subclassed UINavigationBar as follows:

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

and assigned it to the appropriate Navigation Controller via the StoryBoard:

CustomNavigationBar

but it makes no difference.

Any ideas?

ppp
  • 713
  • 1
  • 8
  • 23

2 Answers2

1

Navigation bar has different height in Portrait and Landscape mode. You should handle content of custom title view according to navigation bar height. Use autolayout to auto adjust the subview when navigation bar's height changes.

Jitendra Singh
  • 2,103
  • 3
  • 17
  • 26
0

I solved it by overriding sizeThatFits func in an extension. The idea is that it resets the size back to 44 which is the default for portrait:

// prevent navigation bars from resizing in landscape
extension UINavigationBar {
    public override func sizeThatFits(size: CGSize) -> CGSize {
        let portraitSize = CGSizeMake(UIScreen.mainScreen().bounds.width, 44)
        return portraitSize
    }
}
ppp
  • 713
  • 1
  • 8
  • 23
  • Unfortunately this doesn't work in Swift 5 - no matter if I start the app with landscape orientation or turn the device later, the navigation bar still cuts off the text. `navigationController!.navigationBar.frame.height` returns "44" no matter what the orientation (even without the extension) is but it's clearly less in lanscape. There are also changes in Swift 5: `open override func sizeThatFits(_ size: CGSize) -> CGSize` and `let portraitSize = CGSize(width: UIScreen.main.bounds.width, height: 44)` – Neph Jun 26 '19 at 12:50