本文介绍了如何为单个段设置 UISegmentedControl 色调颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
开始学习 Swift 并尝试转换此 ObjectiveC 代码:
Starting to learn Swift and am attempting to convert this ObjectiveC code:
[[mySegmentedControl.subviews objectAtIndex:0] setTintColor:[UIColor blueColor]]
这正确设置了第一段的色调.
This correctly sets the tint color of the first segment.
这是我最接近获得相同代码的 Swift 版本:
This is the closest I've come to getting a Swift version of the same code:
mySegmentedControl?.subviews[0].tintColor = UIColor.blueColor()
我得到的错误是 '@Ivalue $T9' 与 'UIColor!!' 不一样
我不明白这个错误是什么意思.当我查看 .tintColor
方法时,它列出了 UIColor!?
并且我还没有发现 !?
在 Swift 中一起意味着什么.
I don't understand what this error means. When I look at the .tintColor
method it list UIColor!?
and I haven't found what the !?
together means in Swift yet.
推荐答案
这将解决您的问题:
var subViewOfSegment: UIView = mySegmentedControl.subviews[0] as UIView
subViewOfSegment.tintColor = UIColor.blueColor()
你也可以
(mySegmentedControl.subviews[0] as UIView).tintColor = UIColor .blueColor()
这篇关于如何为单个段设置 UISegmentedControl 色调颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!