UIImageView.appearance 覆盖 UISegmentedControl.appearance

UIImageView.appearance is overriding UISegmentedControl.appearance

我一直在尝试使用外观代理 API 将一些默认颜色应用于某些控件,但我 运行 遇到了问题。

当我使用诸如...

之类的东西将色调颜色应用到 UISegmentedControl
UISegmentedControl.appearance().tintColor = UIColor.red

它生成这个...

很好,但是当我添加...

UIImageView.appearance().tintColor = UIColor.green 

它变为...

澄清一下,我的代码中有这两行

UISegmentedControl.appearance().tintColor = UIColor.red
UIImageView.appearance().tintColor = UIColor.green

不管我按什么顺序调用它们,结果都是一样的,UIImageView 属性覆盖 UISegmentedControls

我花了半天多的时间试图找到解决这个问题的方法,但似乎找不到任何有效的方法。

运行Xcode8.2,iOS10,Swift3

我做错了什么,我该如何解决?

我不确定,但我想,UISegmentedControl 使用 UIImageView 创建段,即我们在分段控件中看到的段是 UIImageViews 而不是 UIViews. UISegmentedControl 甚至有为特定片段设置图像的方法。

如果以上为真,我们可以使用 UIAppearance 的 appearanceWhenContainedIn API 来设置图像视图色调颜色,如下所示:

UIImageView.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).tintColor = UIColor.red
UIImageView.appearance().tintColor = UIColor.green