Set UIBarButtonItem.appearance() not work on iOS 15

Viewed 1208

How to set global appearance UIBarButtonItem on UINavigationBar

UIBarButtonItem.appearance() not work on iOS 15

let BarButtonItemAppearance = UIBarButtonItem.appearance()
let attributes = [NSAttributedString.Key.font : UIFont(name:"Futura-Medium", size: 14) as Any]
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)                
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

screenshot

1 Answers

On iOS 15 it seems you must use the UINavigationBarAppearance object instead (which in my opinion is far from being an improvement overall)

if #available(iOS 15.0, *) {
    let navigationAppearance = UINavigationBarAppearance()
    navigationAppearance.configureWithDefaultBackground()
    let buttonAppearance = UIBarButtonItemAppearance()
    buttonAppearance.normal.titleTextAttributes = attributes
    buttonAppearance.highlighted.titleTextAttributes = attributes
    navigationAppearance.buttonAppearance = buttonAppearance

    let appearance = UINavigationBar.appearance()
    appearance.standardAppearance = navigationAppearance
    appearance.scrollEdgeAppearance = navigationAppearance
} else {
    let appearance = UIBarButtonItem.appearance()
    appearance.setTitleTextAttributes(attributes, for: .normal)
    appearance.setTitleTextAttributes(attributes, for: .highlighted)
}
Related