Gradient not covering full frame SWIFT

随声附和 提交于 2019-12-11 15:16:52

问题


I have the following extension to 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)

on applying this to the imageView, I am having the following output. I tried setting the constraints and also, the autolayout.

Following is the output after the execution:


回答1:


You need to call

imageView.addBlackGradientLayer(frame: imageView.bounds)

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



来源:https://stackoverflow.com/questions/46753710/gradient-not-covering-full-frame-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!