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

以下是执行后的输出:

ios - 渐变不覆盖全帧SWIFT-LMLPHP

最佳答案

你需要打电话

imageView.addBlackGradientLayer(frame: imageView.bounds)


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

关于ios - 渐变不覆盖全帧SWIFT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46753710/

10-12 07:34
查看更多