[英]UIBarButtonItem tint color not working
我最近注意到tintColor
属性的行为在最近的一次iOS 10更新中发生了显着变化。
我有一个以前完美运行的代码,现在根本不工作。
在我的一个应用程序中,我有一组UIBarButtonItem
,它们被添加到视图控制器的toolbarItems
数组中。 它们显示得很好。
但是,在某些时候,我想要更改某些项目的色调颜色以响应用户操作。 代码很简单:
flagButton.tintColor = [UIColor redColor];
这个代码以前用过,但现在它不起作用! 我已经验证了存在对UIBarButtonItem
的引用,并且执行了这行代码。 但是,色调颜色没有改变。
在tintColor
属性的描述中,它说:
要在此属性更改时刷新子视图呈现,请覆盖tintColorDidChange()方法。
我尝试添加tintColorDidChange
方法,但没有调用它。 我在这个阶段的假设是,一旦tintColor
被更改,我的按钮就不会自动刷新。
有人可以帮忙找到手动刷新它的方法吗? 该按钮是带有图像的标准UIBarButtonItem
。
实际上我注意到,如果我更改工具栏的tintColor
而不是单个项目代码正在工作。 似乎由于某种原因,在单个项目上设置色调颜色不再有效。 有谁知道为什么会这样?
由于matt的提示,我能够找到问题的根源。 它是由启动应用程序时执行的代码引起的:
[[UITableViewCell appearance] setTintColor:[ColourTheme sharedTheme].navBackground];
现在,我完全不知道为什么这一行导致了UIBarButtonItem
色调的问题...这可能是因为它在UIAppearance
引入了一些含糊之UIAppearance
。 无论如何,由于我自己不再需要这条线,我可以轻松地将其移除。 我仍然会打开我的问题,因为我认为这种行为非常奇怪,所以在某种程度上,这个问题仍然没有答案。
就像一个额外的信息 - 遇到这些问题的视图控制器包含一个表视图。
我遇到了同样的问题,最后通过以下步骤解决了这个问题:
1)创建条形按钮项目的插座。 我被称为“closeButton”2)写下面的代码:
override func viewWillAppear(animated: Bool) {
let barButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
closeButton.setTitleTextAttributes(barButtonAttributes, for: .normal)
}
请务必将代码放在viewWillAppear中,因为这会覆盖UIAppearance的默认设置。
所以在我的情况下,这一行
UIActivityIndicatorView.appearance.tintColor = FlatWhite;
在didFinishLaunchingWithOptions中设置将所有UIBarItemItems.tintColor更改为默认蓝色,除了在父栏上设置色调颜色之外,无法以其他方式更改它...
当我评论这一行时,一切正常,当取消注释它停止工作。 对我来说,这是IOS框架中的明显错误。 这很难预测,因为在其他情况下它没有什么不同。
我猜你正在使用UIImageRenderingMode.alwaysOriginal,它会导致问题。
public enum UIImageRenderingMode : Int {
case automatic // Use the default rendering mode for the context where the image is used
case alwaysOriginal // Always draw the original image, without treating it as a template
case alwaysTemplate // Always draw the image as a template image, ignoring its color information
}
请改用.alwaysTemplate
如果您在导航控制器中嵌入了视图控制器 - 在导航控制器中,检查导航栏视图/色调属性。 我发现这会覆盖为View Controller添加到ViewDidLoad中的任何代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.