iosuinavigationcontrolleruinavigationitempreferslargetitles

Navigation Bar Title switches from Small to Large text after switching ViewControllers and scrolling down?


I just noticed this issue in my app. I don't wan't to have Large titles at all, so this is the code that I'm calling in my viewDidLoad()...

//MARK: - Navigation Bar Setup
    func navConAcc() {
        let addBarButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addWorkout))
        navigationItem.rightBarButtonItem = addBarButton
        navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(red: 0.4784, green: 0.0863, blue: 0, alpha: 1.0)]
        navigationController?.navigationBar.prefersLargeTitles = false
        navigationController?.navigationItem.largeTitleDisplayMode = .never
        navigationItem.title = "My workouts"
    }

As you can see, I have large titles set to false and display mode set to .never, so when this viewController loads up for the first time, there are no issues with the title size, but if I segue to the next viewController where I have prefersLargeTitles set to true, and then I subsequently go back to the first viewController and scroll down, the title in my first viewController switches to Large for some reason. Can someone explain to me why this happens?


Solution

  • It's because you set the prefered behaviour of nav-bar on the second controller to be large and that is maintained because it's in the same navigational-stack. Add this code to your first view controller to fix this:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.prefersLargeTitles = false
    }