UIImageView.appearance is overriding UISegmentedControl.appearance

♀尐吖头ヾ 提交于 2020-01-05 01:08:11

问题


I've been trying to use the appearance proxy API to apply some default colors to some controls, but I've run into a problem.

When I apply a tint color to UISegmentedControl using something like...

UISegmentedControl.appearance().tintColor = UIColor.red

It generates this...

All good, but when I add...

UIImageView.appearance().tintColor = UIColor.green 

it changes to...

Just to be clear, I have BOTH this lines in my code

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

It doesn't matter in what order I call them, the result is the same, the UIImageView properties override the UISegmentedControls

I've spent over half a day trying to find a solution to this problem but can't seem to find anything that works.

Running Xcode 8.2, iOS 10, Swift 3

What am I doing wrong and how can I fix?


回答1:


I am not sure about this, but I guess, UISegmentedControl uses UIImageView to create segments, i.e. the segments we see inside segmented control are UIImageViews and not UIViews. UISegmentedControl even has methods to setImage for a particular segment.

If above is true, we can use appearanceWhenContainedIn API of UIAppearance to set image view tint colour like this:

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


来源:https://stackoverflow.com/questions/42062659/uiimageview-appearance-is-overriding-uisegmentedcontrol-appearance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!