我对imageView
有以下扩展名:
extension UIImageView {
func addBlackGradientLayer(frame: CGRect){
let gradient = CAGradientLayer()
gradient.frame = frame
gradient.colors = [UIColor.clear.cgColor, UIColor.black.cgColor]
gradient.locations = [0.0, 1.0]
self.layer.addSublayer(gradient)
}}
imageView.addBlackGradientLayer(frame: imageView.frame)
将此应用到imageView,我有以下输出。我尝试设置约束以及自动布局。
以下是执行后的输出:
答案 0 :(得分:1)
您需要致电
imageView.addBlackGradientLayer(frame: imageView.bounds)
请参阅:Cocoa: What's the difference between the frame and the bounds?