条形按钮项目色调颜色不起作用 [英] Bar Button Item Tint Color not working

查看:24
本文介绍了条形按钮项目色调颜色不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您在附加图像中所见,条形按钮的色调颜色设置为红色,但显示为黑色.我认为这是因为文本颜色仍然是黑色?有没有办法改变这种情况?

As you can see in the attached image, the bar button has its tint color set to red, however it is displayed in black. I think this is due to the text color still being black? Is there a way to change this?

我更改了此栏按钮项的色调颜色、它所在的导航栏、UIBarButtonItem.appearance() 以及 UINavigationBar.appearance().关闭弹出窗口并重新打开它会改变颜色.

I changed the tint color of this bar button item, the navigation bar it is in, and the UIBarButtonItem.appearance(), as well as UINavigationBar.appearance(). Closing the popover and reopening it changes the color.

编辑

在栏按钮下方的层次结构中,具有正确的色调颜色(无文本),标签具有正确的色调和文本颜色,但标签具有不同的色调和文本颜色.文本颜色仍然是旧主题中的颜色,并且在视图关闭并重新打开之前不会更新.

In the hierarchy below the bar button has the correct tint color (no text), the label has correct tint and text colors however the label has different tint and text colors. Text color is still the one from the old theme and does not update until the view is dismissed and reopened.

但是按住按钮会使其以新颜色闪烁,然后逐渐恢复正常.

However holding down the button causes it to flash in the new color, and then fade back to normal.

推荐答案

这是我用来设置 UIBarButtonItem 样式的一段代码:

Here is a piece of code I use to style 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)
   }

}

这本该结束,但尝试重复更改标题属性和色调(例如按钮操作)不会显示任何内容,除非栏按钮项的标题更改.这是一种奇怪的行为,但一个简单的解决方法就是这样做,通过添加和删除标题末尾的额外空格来更改标题,例如:

This should've been the end of it but attempting to change the title attributes and tint color repeatedly (e.g. on button action) reveals nothing happens unless the bar button item's title changes. This is weird behaviour but an easy workaround will be to do just that, change the title by adding and removing extra space at the end of the title like:

// 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 + " "

这篇关于条形按钮项目色调颜色不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆