How to change font size of Nav Bar on back pressed?

Viewed 44

Okay this is a problem that's been frustrating me for a bit. I have two screens in a nav controller one's bar needs to be a big font size (bigFontVC), one needs to be a small font size (smallFontVC).

I go from bigFontVC to smallFontVC with a segue. The smallFontVC's viewDidLoad is:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.setToNormalFont() 
}

and bigFontVC´s viewDidLoad is:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.setToSmallFont() 
}

the resize works perfectly from the segue from bigFontVC to smallFontVC.

Now going from smallFontVC to bigFontVC is more difficult. I know the bigFontVC doesn't go out of load so viewDidLoad doesn't get called. Now I'm trying to work out where to put my function

    self.navigationController?.setToNormalFont() 

to resize BigFontVC's navBar font size.

The problem is the places I first thought to put this function cause the navigation bar to do the font switch but it shows first only half the title. e.g 'GAM' and then a split second later 'GAMES'. It just doesn't look good to have this on every change. As this didn't happen going from bigFontVC to smallFontVC i'm sure I can make it not happen going the other way, but that is where i'm asking for help.

Below are the places I have tried to put that function which cause the title to flash and not change smoothly.

In BigFontVC the following place:

override func viewWillAppear(_ animated: Bool) {
     self.navigationController?.setToNormalFont()

}

In SmallFontVC the following place:

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.setToNormalFont()
}

This I know is a weird problem it may be something much deeper in navigation controller that I don't know. I was thinking maybe somehow overriding the back button to make it perform a segue as I know with a segue there wasn't this problem. Any help would be appreciated even if it's just to tell me it's not possible.


Also in case it helps this is my navcontroller function:

extension UINavigationController {
    func setToNormalFont(){
        setFont(as:  UIFont(name: "futura", size: 19)!)
    }
    func setToSmallFont(){
        setFont(as:  UIFont(name: "futura", size: 16)!)
    }
    func setFont(as font: UIFont){
        let titleAttributes = [
            NSFontAttributeName: font
        ]
        self.navigationBar.titleTextAttributes = titleAttributes
    }
}
0 Answers
Related