【发布时间】:2019-07-02 06:05:55
【问题描述】:
我在情节提要中创建了一个UIViewController。有一个红色的UILabel。经过一些活动后,我将标签上的文本更改为 textColor 为绿色。
奇怪的是:当我更改设备方向标签时,textColor 会变回原来的红色,但文字仍然存在。
如何让标签上的textColor 在设备旋转后保持绿色?
class ViewController: UIViewController {
@IBOutlet private weak var volumeButton: UIButton!
@IBOutlet private weak var clLabel: UILabel!
override var traitCollection: UITraitCollection {
if view.bounds.width < view.bounds.height {
let traits = [UITraitCollection(horizontalSizeClass: .compact), UITraitCollection(verticalSizeClass: .regular)]
return UITraitCollection(traitsFrom: traits)
} else {
let traits = [UITraitCollection(horizontalSizeClass: .regular), UITraitCollection(verticalSizeClass: .compact)]
return UITraitCollection(traitsFrom: traits)
}
}
@IBAction private func handleInputVolume(_ sender: UIButton) {
coordinator?.inputData(type: .volume, delegate: self)
}
}
extension ViewController: InputDataReceivable {
func didFinishInput(volume: Double) {
volumeButton.setTitle(volume.formattedString, for: .normal)
volumeButton.setTitleColor(.enabledGreen, for: .normal)
clLabel.textColor = .enabledGreen
}
}
【问题讨论】:
-
能贴出代码吗?
-
您的标签是否在
UITableView或UICollectionView内? -
标签只是在 ViewController 的视图中。在情节提要中使用自动布局。