let attributes = [
NSAttributedString.Key.foregroundColor: UIColor.orange,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 22)
]
// set nav bar button item
// solution 1 - this will not work properly.
// refer to the Imgur
UINavigationBar.appearance().titleTextAttributes = attributes
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(attributes, for: .normal)
//
// solution 2: this works fine
let app = UIBarButtonItemAppearance()
app.normal.titleTextAttributes = attributes
let navBarApp = UINavigationBarAppearance()
navBarApp.configureWithOpaqueBackground()
navBarApp.buttonAppearance = app
UINavigationBar.appearance().standardAppearance = navBarApp
I am not sure why solution 1 doesn't fully work but I think the issue is related to this line UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(attributes, for: .normal)
. Is this not the right way to set appearance and styling(font/color/etc) for navBar and BarButtonItem?
Solution 2 works fine so I am not going to attach anything.
Edit: looks like the link expired. Here's the new Link for solution 1: https://i.sstatic.net/4ZKDS.jpg
I managed to fix the issue for solution 1, but honestly have no idea why I need both normal
and highlighted
state for it to work properly.
Need to add the following
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(attributes, for: .highlighted)