在iOS 13上更改UIBarButtonItem字体

时间:2019-09-21 10:47:03

标签: ios swift uinavigationbar

UIBarButtonItem.appearance().setTitleTextAttributes()在iOS 13上不起作用。

1 个答案:

答案 0 :(得分:3)

iOS处理全局外观的方式似乎已经改变。您需要为iOS 13及更高版本添加条件检查,然后添加属性,如下所示。

if #available(iOS 13.0, *) {
    let standard = UINavigationBarAppearance()
    standard.configureWithTransparentBackground()

    // TITLE STYLING
    standard.titleTextAttributes = [.foregroundColor: UIColor.white, .font: "FONTNAME"]

    // NAV BUTTON STYLING
    let button = UIBarButtonItemAppearance(style: .plain)
    button.normal.titleTextAttributes = [.foregroundColor: .white, .font: "FONTNAME"]
    standard.buttonAppearance = button

    let done = UIBarButtonItemAppearance(style: .done)
    done.normal.titleTextAttributes = [.foregroundColor: .white, .font: "FONTNAME"]
    standard.doneButtonAppearance = done

    UINavigationBar.appearance().standardAppearance = standard
} else { 

    // Your previous styling here for 12 and below

}

查看this帖子以了解更多信息。我发现它对于理解新更新非常有用。