我正在尝试以编程方式向UIView添加渐变,但它没有起作用。它似乎根本没有颜色。我附上了相关的代码和屏幕截图。请注意,我正在应用渐变的底部正方形。有人可以帮助我找出我在这里做错了什么吗?
let sundayView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupSundayView()
}
func setupViews() {
sundayView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(sundayView)
}
func setupSundayView() {
sundayView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints([
sundayView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor),
sundayView.topAnchor.constraintEqualToAnchor(fridayView.bottomAnchor, constant: 16.0),
sundayView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -8.0),
sundayView.heightAnchor.constraintEqualToAnchor(mondayView.heightAnchor),
sundayView.widthAnchor.constraintEqualToAnchor(mondayView.widthAnchor)
])
let gradient = CAGradientLayer()
gradient.frame = sundayView.bounds
gradient.colors = [
UIColor(red:1.00, green:0.37, blue:0.23, alpha:1.0).CGColor,
UIColor(red:1.00, green:0.16, blue:0.41, alpha:1.0).CGColor
]
sundayView.layer.insertSublayer(gradient, atIndex: 0)
}
QuartzCore
框架了? - Evgeny Karkan