ios - UIBarButtonItem 色调颜色不起作用

标签 ios cocoa-touch ios10 uibarbuttonitem tintcolor

我最近注意到 tintColor 属性的行为在最近的 iOS 10 更新之一中发生了显着变化。

我有一段代码,以前运行良好,现在根本无法运行。

在我的一个应用程序中,我有一个 UIBarButtonItem 集合,它们被添加到 View Controller 的 toolbarItems 数组中。它们显示良好。

但是,有时我想更改某些项目的色调以响应用户操作。代码很简单:

flagButton.tintColor = [UIColor redColor];

这段代码以前可以用,现在不行了!我已验证对 UIBarButtonItem 的引用存在并且这行代码已执行。然而,色调颜色并没有像它应该的那样改变。

tintColor 属性的描述中它说:

To refresh subview rendering when this property changes, override the tintColorDidChange() method.

我尝试添加 tintColorDidChange 方法,但它没有被调用。我在这个阶段的假设是,一旦 tintColor 改变,我的按钮就不会自动刷新。

有人可以帮忙找到一种手动刷新它的方法吗?该按钮是带有图像的标准 UIBarButtonItem

更新1

实际上我注意到,如果我更改工具栏的 tintColor 而不是单个项目,代码就可以正常工作。似乎由于某种原因,在单个项目上设置色调颜色不再有效。有谁知道为什么会这样?

更新2

多亏了 matt 的提示,我才能够找到问题的根源。这是由应用程序启动时执行的代码引起的:

[[UITableViewCell appearance] setTintColor:[ColourTheme sharedTheme].navBackground];

现在,我完全不明白为什么这一行会导致 UIBarButtonItem 色调出现问题...这可能是因为它在 UIAppearance 中引入了一些歧义。无论如何,因为我自己不再需要这条线,所以我可以轻松地将其删除。我仍然会保留我的问题,因为我认为这种行为非常奇怪,所以在某种程度上,这个问题仍然没有答案。

作为附加信息 - 遇到这些问题的 View Controller 中包含一个 TableView 。

最佳答案

我无法重现任何问题。在这里您可以看到我们正在交替使用红色和绿色之间的右侧栏按钮项目的色调;这是通过直接在代码中设置条形按钮项目的 tintColor 来完成的:

enter image description here

        let c = self.replyButton.tintColor
        if c == UIColor.red {
            self.replyButton.tintColor = .green
        } else {
            self.replyButton.tintColor = .red
        }

因此我得出结论,您所看到的问题是由您未告诉我们的其他原因引起的。

关于ios - UIBarButtonItem 色调颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41743871/

相关文章:

ios - Google API 客户端使用 - API key 是强制性的吗?

iphone - 我的应用程序在 App Store 上没有显示任何评论和评分?

cocoa - CGGeometry、CGPoint、CGRect中的 "CG"代表什么?

ios - 是否可以在后台检测用户的移动事件?

swift3 - 将视频录制到特定相册

iOS 10 不调用通知服务扩展

ios - 有没有办法找到 XCUIElement 是否有焦点?

ios - UICollectionView - 删除所有项目或更新以刷新它

iphone - 试图从主线程或 Web 线程以外的线程获取 Web 锁。现在崩溃

ios - 在 iOS 10 上的 WKWebView 中打开 PDF 文件时启用双指缩放手势