Search code examples
iosswiftxcodexcode11.4

Navigation Bar title font problem on ios 13


I’m using Xcode 11.4 and iOS 13.4. I have set navigation bar title custom font using UINavigatinBar.appearance() And it works correctly but on iOS 13+ when i try to push to another VC and then comeback to the parent VC, the parent VC title font suddenly has been set to default font and after a second it changes back to the custom font.

Below is a gif of the problem:

nav bar font problem


Solution

  • iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems

    Check this code, might help you

        let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
        let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
    
        UINavigationBar.appearance().tintColor = UIColor.white // bar icons
    
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .red // If you want different nav background color other than white
    
            appearance.titleTextAttributes = titleFontAttrs
            appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
    
            UINavigationBar.appearance().isTranslucent = false
    
            appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
            appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
    
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        } else {
            UINavigationBar.appearance().barTintColor = .red // bar background
    
            UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
    
            UINavigationBar.appearance().isTranslucent = false
    
            UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
            UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
        }