IOS add / remove shadow from view

I don’t understand how to remove the shadow added to the view. I add a shadow to the view in initWithFrame as follows:

 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 running the application, I want to remove the shadow from this view. I tried using:

 layer.hidden = YES; 

or

 self.layer.hidden = YES; 

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

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

+13
ios uiview
source share
4 answers

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

So this should work:

 self.layer.shadowOpacity = 0.0; 

See the CALayer shadowOpacity page shadowOpacity

And to show the use of shadow:

 self.layer.shadowOpacity = 1.0; 
+32
source share

Sorry, I'm not sure, but have you tried changing the properties of the layer shadow ? For example, one of them:

  layer.shadowOffset = CGSizeMake(0, 0); layer.shadowColor = [[UIColor clearColor] CGColor]; layer.cornerRadius = 0.0f; layer.shadowRadius = 0.0f; layer.shadowOpacity = 0.00f; 
+7
source share

Swift 4.2

I use this in my code for shortcuts and navigation bar.

 extension UIView { func shadow(_ height: Int = 5) { self.layer.masksToBounds = false self.layer.shadowRadius = 4 self.layer.shadowOpacity = 1 self.layer.shadowColor = UIColor.gray.cgColor self.layer.shadowOffset = CGSize(width: 0 , height: height) } func removeShadow() { self.layer.shadowOffset = CGSize(width: 0 , height: 0) self.layer.shadowColor = UIColor.clear.cgColor self.layer.cornerRadius = 0.0 self.layer.shadowRadius = 0.0 self.layer.shadowOpacity = 0.0 } } 
+2
source share

The β€œlayer” you are trying to make hidden is the layer of the object in which you have a shadow to it, not a visible aspect .. only objects with a layer ... it's pretty confusing to conceptualize anyway, the only way to remove the shadow - this is to undo what you originally did, as suggested above, there is no specific property that you can simply switch to bool and remove the shadow.

+1
source share

All Articles