【发布时间】:2017-06-08 07:26:13
【问题描述】:
我最近尝试向我的一个 Swift 类 (ScoreView) 添加符合 UIApperance 的属性,但没有成功。相关代码如下:
private var gaugeThresholds = Array<GaugeThreshold?>(repeating: nil, count: ScoreView.maxGaugeIntervals)
public func gaugeThreshold(forInterval interval: Int) -> GaugeThreshold? {
return (0 <= interval && interval < ScoreView.maxGaugeIntervals) ? self.gaugeThresholds[interval] : nil
}
public func setGaugeThreshold(_ threshold: GaugeThreshold, forInterval interval: Int) {
if 0 <= interval && interval < ScoreView.maxGaugeIntervals {
self.gaugeThresholds[interval] = threshold
}
}
GaugeThreshold 是一个 Swift 结构体:
public struct GaugeThreshold {
let until: Float
let color: UIColor
public init(until: Float, color: UIColor) {
self.until = until
self.color = color
}
}
这失败得很惨,我能想到的失败的唯一原因是我为属性使用了自定义类型而不是标准类型之一。
根据UIAppearanceContainer文档:属性类型可以是任何标准的iOS类型:id、NSInteger、NSUInteger、CGFloat、CGPoint、CGSize、CGRect、@987654335 @ 或 UIOffset。但是,如果您浏览list of methods and properties conforming to UIAppearance as of iOS 8.0,您会发现还有其他类型可以正常工作,例如UIImage、UIColor... 那么有人知道是什么让一个类型可以用作UIAppearance 属性吗?
【问题讨论】:
标签: ios swift uiappearance