Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS add / remove shadow from a view

Tags:

I do not understand how to remove a shadow that was added to a view. I add to my view in initWithFrame a shadow in this way:

self.layer.borderWidth = 2; self.layer.borderColor = [UIColor clearColor].CGColor; self.backgroundColor = [UIColor greenColor]; [self.layer setCornerRadius:8.0f]; CALayer *layer = self.layer; layer.shadowOffset = CGSizeMake(2, 2); layer.shadowColor = [[UIColor blackColor] CGColor]; layer.cornerRadius = 8.0f; layer.shadowRadius = 3.0f; layer.shadowOpacity = 0.80f; layer.shadowPath = [[UIBezierPath bezierPathWithRect:layer.bounds] CGPath]; 

After in the execution of the app I want to remove the shadow from this view. I've tried using:

layer.hidden = YES; 

or

self.layer.hidden = YES; 

but this hides the view completely, not just the added shadow.

Is there a way to retrieve the added shadow from a view and then hide it? Thanks!

like image 636
Hw.Master Avatar asked May 25 '13 15:05

Hw.Master


People also ask

What is shadow opacity in Swift?

shadowColor sets the color of the shadow, and needs to be a CGColor . shadowOpacity sets how transparent the shadow is, where 0 is invisible and 1 is as strong as possible. shadowOffset sets how far away from the view the shadow should be, to give a 3D offset effect. shadowRadius sets how wide the shadow should be.

What is the purpose of IOS shadow?

The layer's shadow uses several elements: shadowOffset , shadowColor , shadowOpacity , and shadowRadius . Each element changes the respective appearance. You can offset the shadow differently to change the direction the shadow is cast - how far from the layer and in which direction.


1 Answers

I guess you could use the shadowOpacity property of your CALayer.

So this should work:

self.layer.shadowOpacity = 0; 

See the CALayer's shadowOpacity documentation page

And to show your shadow use:

self.layer.shadowOpacity = 1.0; 
like image 58
Guillaume Algis Avatar answered Nov 20 '22 16:11

Guillaume Algis