Search code examples
iosswiftuinavigationcontrolleruinavigationbaruistoryboardsegue

why I cannot change the font of my back button in UINavigationController?


I want to change the text and font of my navigation controller.

I have a static UITableView and when user clicks one cell - I go to the 2nd UIViewController through segue.

I set up the name of the segue in the storyboard and in the code I have:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "myTestSegue") {
        if segue.destination is MyTest {
            print("can I see it?")
        let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: nil)
        backButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AppleSDGothicNeo-Light", size: 20.0)!], for: UIControlState())
        navigationItem.backBarButtonItem = backButton
        }
    }
}

now when I run the app and click the cell, I see in the console: can I see it?, but the text and font of the button on the next panel doesn't change.

What am I missing?


Solution

  • you can change font style of back button with this code.

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
     setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor],
       NSShadowAttributeName:shadow,
       NSFontAttributeName:[UIFont boldSystemFontOfSize:15.0]
       }
     forState:UIControlStateNormal];