ash4
ash4

Reputation: 101

iOS - Navigation bar does not show large titles when orientation changed

I have largeTitle on my navigation bar which shows correctly.

WHen I change my device orientation to landscape and back to portrait, it becomes regular instead of largeTitle.

This is a tab bar controller. When I switch tabs (to Calendar as in below gif)then the view reloads and shows it correctly

In my tab bar controller I have added this but isn't helping me to readjust largeTitle in navigation bar

override func viewWillTransition(to size: CGSize, with coordinator: 
  UIViewControllerTransitionCoordinator) {
          if UIDevice.current.orientation.isLandscape {
             //do something
          } else {

  self.navigationController?.navigationBar.prefersLargeTitles = true
              self.navigationItem.largeTitleDisplayMode = .always
          }
      }

If i add above code in my tabviewcontroller it doesn't even call. It calls from my tabbar controller but itsn't updating titles to large

I'm struck here. Please let me know how i can fix this

Thanks enter image description here

Upvotes: 2

Views: 688

Answers (1)

Pavel Kozlov
Pavel Kozlov

Reputation: 1003

You can reset your navigation bar height to default one on changing device orientation (here I reset the whole navigation bar frame for simplicity):

class ViewController: UIViewController {

    lazy var navigationControllerLargeTitleFrame: CGRect = {
        let navigationController = UINavigationController()
        navigationController.navigationBar.prefersLargeTitles = true
        return navigationController.navigationBar.frame
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChangeNotification), name: UIDevice.orientationDidChangeNotification, object: nil)
    }

    @objc func orientationDidChangeNotification(_ notification: NSNotification) {
        if UIDevice.current.orientation == .portrait {
            navigationController?.navigationBar.frame = navigationControllerLargeTitleFrame
        }
    }

}

Upvotes: 1

Related Questions