正如您在附图中看到的那样,条形按钮的色调颜色设置为红色,但它以黑色显示。我认为这是由于文字颜色仍然是黑色的?有没有办法改变这个?
我更改了此条形按钮项目的色调颜色,它所在的导航栏以及UIBarButtonItem.appearance()
和UINavigationBar.appearance()
。关闭弹出框并重新打开它会改变颜色。
修改
在下面的层次结构中,条形按钮具有正确的色调(无文本),标签具有正确的色调和文本颜色,但标签具有不同的色调和文本颜色。文本颜色仍然是旧主题中的颜色,并且在视图被取消并重新打开之前不会更新。
然而,按住该按钮会使其以新颜色闪烁,然后淡出恢复正常。
答案 0 :(得分:1)
以下是我用来设置UIBarButtonItem
样式的一段代码:
extension UIBarButtonItem {
func style(tint color: UIColor, font f: UIFont? = nil) {
let font = f ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
tintColor = color
let atts = [
NSAttributedStringKey.font : font,
NSAttributedStringKey.foregroundColor : color
]
setTitleTextAttributes(atts, for: .normal)
}
}
这应该是它的结束但是尝试重复更改标题属性和色调颜色(例如,在按钮操作上),除非条形按钮项目的标题发生变化,否则不会发生任何事情。这是奇怪的行为,但一个简单的解决方法就是这样做,通过添加和删除标题末尾的额外空格来更改标题,如:
// Hack!!! adds and removes an empty space to the title to
// force the bar item reset title attributes.
let title: String = barItem.title ?? ""
barItem.title = title.hasSuffix(" ") ? String(title.dropLast()) : title + " "