我对UIView进行了扩展以应用渐变:
extension UIView {
func applyGradient(colors: [CGColor]) {
self.backgroundColor = nil
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds // Here new gradientLayer should get actual UIView bounds
gradientLayer.cornerRadius = self.layer.cornerRadius
gradientLayer.colors = colors
gradientLayer.startPoint = CGPoint(x: 0.0,y: 0.0)
gradientLayer.endPoint = CGPoint(x: 1.0,y: 1.0)
gradientLayer.masksToBounds = true
self.layer.insertSublayer(gradientLayer,at: 0)
}
}
在我的UIView子类中,我正在创建所有视图并设置约束:
private let btnSignIn: UIButton = {
let btnSignIn = UIButton()
btnSignIn.setTitle("Sing In",for: .normal)
btnSignIn.titleLabel?.font = UIFont(name: "Avenir Medium",size: 35)
btnSignIn.layer.cornerRadius = 30
btnSignIn.clipsToBounds = true
btnSignIn.translatesAutoresizingMaskintoConstraints = false
return btnSignIn
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubViews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
addSubViews()
}
func addSubViews() {
self.addSubview(imageView)
self.addSubview(btnSignIn)
self.addSubview(signUpstackView)
self.addSubview(textFieldsStackView)
setConstraints()
}
我已经重写了layoutSubviews函数,该函数在每次更改视图范围(包括定向转换)时调用,在这里我称为applyGradient。
override func layoutSubviews() {
super.layoutSubviews()
btnSignIn.applyGradient(colors: [Colors.ViewTopGradient,Colors.ViewBottomGradient])
}
问题在于定向过渡渐变由于某种原因而应用错误...
我在这里想念什么?