Navigation Bar title font problem on ios 13
Asked Answered
A

2

1

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

Adoree answered 28/3, 2020 at 15:6 Comment(0)
C
7

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)
    }
Clariceclarie answered 10/5, 2020 at 13:6 Comment(5)
Weird, I'm using iOS 14 & Xcode 12, above code is not take effect at all.Peptidase
That's strange, I've defined the above in UINavigationController extensionClariceclarie
@Clariceclarie facing same issue with xCode12 and iOS 14Flatt
@pawan_kumar the navigationBar background is not changing for you or the bar button attributes? Or both?Clariceclarie
@Clariceclarie I was checking only navigation title. I used UILabel.Flatt
P
6

Here you go, manage it in viewDidAppear:

let lblTitle = UILabel()

let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
                                                .foregroundColor: UIColor.black]

let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)

lblTitle.attributedText = attributeString

lblTitle.sizeToFit()
navigationItem.titleView = lblTitle
Pilgrimage answered 28/3, 2020 at 16:7 Comment(3)
Thanks it works perfectly. BTW do you know why the other approach doesn't work suddenly? It's really odd.Adoree
Its a bug with Apple. Make sure to add in the feedback reporter on mac so a future version of xcode will compile this properlyIrrelative
and the bug still persists. Great company, seriously. Thank you.Suziesuzuki

© 2022 - 2024 — McMap. All rights reserved.