0

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:

enter image description here

Aakash Dave
  • 866
  • 1
  • 15
  • 30

1 Answers1

1

You need to call

imageView.addBlackGradientLayer(frame: imageView.bounds)

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

D. Mika
  • 2,577
  • 1
  • 13
  • 29
  • Hey i already tried it. Well, if I run this on a iphone simulator 7+, It shows this issue. While the gradient is properly visible when I run it on my 6s and 7+ devices. Can it be a simulator issue? – Aakash Dave Oct 15 '17 at 10:29
  • I'm not able to reproduce your error on a 7+ simulator. – D. Mika Oct 15 '17 at 11:08
  • Maybe your view gets resized after the gradient layer is applied. I suggest checking the frame when the layer is added and after the view did show. – D. Mika Oct 15 '17 at 11:32
  • Yeah its printing the same frame size. The issue is the frame only. I wonder why is it behaving this way, Even though appropriate constraints are set – Aakash Dave Oct 15 '17 at 12:31
  • I am using a collection header view and have a image view that completely wraps the bounds of the header. On printing the frame and bound values, It return the same value. I really dont know what am I doing wrong – Aakash Dave Oct 15 '17 at 12:57
  • I suggest to debug the view hierarchy after the view has been displayed. Check the views frame when it's visible via the debugger and print it's layer's sublayer property. – D. Mika Oct 15 '17 at 13:18
  • you are right! My view is somehow screwing up. I'll keep you posted. – Aakash Dave Oct 15 '17 at 14:54