@IBDesignable并没有在" old"项目

时间:2017-07-20 14:40:54

标签: xcode ibdesignable

我有UIView个子类,例如:

@IBDesignable
class GradientView: UIView {

    @IBInspectable var firstColor: UIColor = UIColor.red
    @IBInspectable var secondColor: UIColor = UIColor.green

    @IBInspectable var vertical: Bool = true

    override func awakeFromNib() {
        super.awakeFromNib()

        applyGradient()
    }

    func applyGradient() {
        let colors = [firstColor.cgColor, secondColor.cgColor]

        let layer = CAGradientLayer()
        layer.colors = colors
        layer.frame = self.bounds
        layer.startPoint = CGPoint(x: 0, y: 0)
        layer.endPoint = vertical ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0)

        self.layer.addSublayer(layer)
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        applyGradient()
    }
}

它成功地在Interface Builder中呈现了一个新项目,但它并不适用于我的" old"项目。 enter image description here

有谁知道为什么会这样?

0 个答案:

没有答案