【问题标题】:What Swift property types are allowed in UIApperance compliant custom classes?在符合 UIApperance 的自定义类中允许哪些 Swift 属性类型?
【发布时间】: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类型:idNSIntegerNSUIntegerCGFloatCGPointCGSizeCGRect、@987654335 @ 或 UIOffset。但是,如果您浏览list of methods and properties conforming to UIAppearance as of iOS 8.0,您会发现还有其他类型可以正常工作,例如UIImageUIColor... 那么有人知道是什么让一个类型可以用作UIAppearance 属性吗?

【问题讨论】:

    标签: ios swift uiappearance


    【解决方案1】:

    当然,没有什么比在五分钟后发布到 StackOverflow 来找出答案更重要的了。原来关键是id 类型。改变

    public struct GaugeThreshold { ...
    

    public class GaugeThreshold: NSObject { ...
    

    成功了。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2017-12-26
      • 2019-12-17
      • 1970-01-01
      • 2013-11-15
      相关资源
      最近更新 更多