【发布时间】:2016-07-16 11:11:10
【问题描述】:
我正在尝试以编程方式向 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框架吗? -
我想没有必要。我尝试将渐变应用于我的超级视图。它无需导入即可工作。
标签: ios swift uiview quartz-core cagradientlayer