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!
Swift 5.+
My solution was to add a
shadowBackgroundView
, which has a removableshadowLayer
. In this way I could easyly remove the layer without resetting the shadow properties.As a Note, for the
UITableViewCell
, you wouldnt need to add ashadowBackgroundView
, but you could add theshadowLayer
directly tocell.view.layer
, which serves as the backgroundView for thecell.contentView
.the "layer" that you are trying to make hidden is the layer of the object that you are having a shadow to it's not a visible aspect.. only the objects with in the layer... it's rather confusing to conceptualize anyways, the only way to remove the shadow is to undo what you originally did, which was suggested above, there is no defined property that you can just toggle a bool and make the shadow go away
Swift 4.2
I am using this in my code for labels and navigation bar.
Sorry, not sure the correct way, but have you tried changing the properties of the
layer shadow
? For example, one of these;I guess you could use the
shadowOpacity
property of yourCALayer
.So this should work:
See the
CALayer
'sshadowOpacity
documentation pageAnd to show your shadow use: