iOS add / remove shadow from a view
Solution 1
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;
Solution 2
Sorry, not sure the correct way, but have you tried changing the properties of the layer shadow
? For example, one of these;
layer.shadowOffset = CGSizeMake(0, 0);
layer.shadowColor = [[UIColor clearColor] CGColor];
layer.cornerRadius = 0.0f;
layer.shadowRadius = 0.0f;
layer.shadowOpacity = 0.00f;
Solution 3
Swift 4.2
I am using this in my code for labels 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
}
}
Solution 4
I tried the other answers, the only thing that worked for me was to toggle layer.masksToBounds
to true/false
lazy var myView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .white
view.layer.cornerRadius = 5
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOpacity = 3
view.layer.shadowOffset = .zero
view.layer.shadowRadius = 5
return view
}()
func showShadow() {
myView.layer.masksToBounds = false
}
func hideShadow() {
myView.layer.masksToBounds = true
}
Solution 5
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
Related videos on Youtube
Hw.Master
Updated on July 09, 2022Comments
-
Hw.Master almost 2 years
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!
-
Guillaume Algis almost 11 yearsIs there a reason you declare a local
layer
variable pointing onself.layer
in the middle of your code ?
-
-
Hw.Master almost 11 yearsThanks very useful i'm not considering this option, but could impact on the performance change this property target then try definitely remove?
-
Shawn Frank almost 3 yearsgreat answer, only thing that seemed to work for me.