我有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"项目。
有谁知道为什么会这样?