swift - 设置 UIBarButtonItem 外观会导致后退按钮出现奇怪的行为
问题描述
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
我不确定为什么解决方案 1不能完全工作,但我认为问题与这条线有关UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(attributes, for: .normal)
。这不是为 navBar 和 BarButtonItem 设置外观和样式(字体/颜色/等)的正确方法吗?
解决方案 2 工作正常,所以我不打算附加任何东西。
编辑:看起来链接已过期。这是解决方案 1 的新链接:https ://imgur.com/a/DJAzrSw
解决方案
我设法解决了解决方案 1 的问题,但老实说,我不知道为什么我需要两者normal
并highlighted
声明它才能正常工作。
需要添加以下内容
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(attributes, for: .highlighted)
推荐阅读
- reactjs - 在表格中找到您单击的行
- java - Gradle - 来自单个项目的多个 Jar
- swift - Swift 发送“地图”作为查询参数
- java - 使用 Retrofit2 for android 解析字符串数组
- sql - 从字符串序列中搜索的 SQL 查询
- zabbix - Zabbix Agent 3.4.9 Active Monitoring Log file, Not supported: too many parameters
- c# - 在第一次记录水晶报告中显示总和
- c# - 如何在部署管道中正确配置数据库和集合?
- azure-service-fabric - Service Fabric 规模集上的存储空间不足
- c++ - 使用 OpenSSL 从内存中读取原始 SSL/TLS 证书