渐变不覆盖全帧SWIFT

时间:2017-10-15 09:47:44

标签: ios swift3 uiimageview

我对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,我有以下输出。我尝试设置约束以及自动布局。

以下是执行后的输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要致电

imageView.addBlackGradientLayer(frame: imageView.bounds)

请参阅:Cocoa: What's the difference between the frame and the bounds?