Swift 4.2 / iOS 12 / Xcode 10。 我在其中创建了一个带有UILabel的自定义视图(名为StepView.xib和StepView.swift),xib文件中的UILabel通过IBOutlet连接到swift文件。然后,我在viewController中使用StepView。当我想在viewController中更改UILabel textColor时,它不起作用,我想知道原因。
class StepView: UIView {
@IBOutlet var labels: [UILabel]!
...
private func setSelected(current phase: ClosedRange<Int>,
select srange: ClosedRange<Int>){
for p in phase {
labels[p].textColor = R.color.mainTextColor()
}
}
// MARK: - Init
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initSubviews()
}
override init(frame: CGRect) {
super.init(frame: frame)
initSubviews()
}
func initSubviews() {
let nib = UINib(nibName: "StepView", bundle: Bundle(for: type(of: self)))
nib.instantiate(withOwner: self, options: nil)
contentView.frame = bounds
addSubview(contentView)
}
}
更多信息: 我在XIB中设置了UILabel的颜色,然后在代码中更改了UILabel的颜色,这是无效的。我将UILabel颜色重置为默认值,然后在代码中更改了UILabel的颜色,这是可行的。我想知道原因。
答案 0 :(得分:0)
设置和重置选定标签和未选定标签的textColors。
private func setSelected(current phase: ClosedRange<Int>,
select srange: ClosedRange<Int>){
for p in phase {
labels[p].textColor = R.color.mainTextColor()
}
for s in srange {
labels[s].textColor = R.color.unSelectedColor()
}
}