UISegmentedContol:反转 tintColor 和 backgroundColor

UISegmentedContol: reverse tintColor and backgroundColor

我有 UISegmentedControl,我在透明背景上添加了一些白色图像。

for (index,element) in ELEMENTS.enumerate() {
    segmentedControl.insertSegmentWithImage(element.logo, atIndex: index, animated: false)
}

未选择的片段现在将背景颜色设置为 segmentedControl.backgroundColor,图像颜色为 segmentedControl.tintColor。所选片段反转,背景设置为 .tintColor,图像使用 .backgroundColor 着色。

这很好用,但我希望它是另一种方式:所选片段的图像颜色为 .tintColor,背景颜色为 .backgroundColor。

我知道我可以通过在代码中切换颜色来实现这一点,但我使用的是

let sharedApplication = UIApplication.sharedApplication()
sharedApplication.delegate?.window??.tintColor = newColor

在应用程序中更改应用程序中所有视图的 tintColor,所以如果这会导致在分段控件中按照我希望的方式更改颜色,那就太好了。

有什么想法吗?

您使用 UIApplication.sharedApplication().delegate?.window??.tintColor 设置应用程序所有控件使用的全局色调颜色。

您可以使用 UISegmentedControl.appearance().tintColor 为应用程序中的所有分段控件设置自定义色调颜色。

并且您可以使用UISegmentedControl.tintColor为特定的分段控件设置自定义色调颜色。

要切换应用程序中所有分段控件的背景色和色调:

UISegmentedControl.appearance().tintColor = backgroundColor
UISegmentedControl.appearance().backgroundColor = tintColor