Shadow does not appear for UIView using CALayer

I have a subclassed UIView loaded from nib and I cannot get a shadow to draw around it. I have been trying to get a shadow to appear around the whole species for quite some time. I decided to place it in my own sublevel in order to simplify its animation later. Here is the code:

 -(void)awakeFromNib { self.clipsToBounds = NO; // set up the shadow layer CALayer *shadow = [CALayer layer]; shadow.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.bounds.size.width, self.bounds.size.height); shadow.shadowColor = [UIColor blueColor].CGColor; shadow.shadowRadius = 15.0; shadow.opacity = 1.0; [self.layer addSublayer:shadow]; // I set this property so I have access to it later to more easily animate it. self.shadowLayer = shadow; } 

When I NSLog shadowLayer property, the coordinates and frame are correct. It corresponds to the view that it supports.

I also set the border color and corner radius to self.layer and it displays correctly. If I put the shadow on self.layer , it will appear, but it covers all the children of my parent UIView .

Any help is greatly appreciated.

+6
source share
3 answers

Suppose you have imported QuartzCore . I think you need to set and create a border with a UIView . The way to use this [self roundedLayerWithShadow:yourView.layer radius:5.0f];

 - (void)roundedLayerWithShadow:(CALayer *)viewLayer radius:(float)r { [viewLayer setMasksToBounds:YES]; [viewLayer setCornerRadius:r]; [viewLayer setBorderColor:[RGB(180, 180, 180) CGColor]]; [viewLayer setBorderWidth:1.0f]; [viewLayer setShadowColor:[RGB(0, 0, 0) CGColor]]; [viewLayer setShadowOffset:CGSizeMake(0, 0)]; [viewLayer setShadowOpacity:1]; [viewLayer setShadowRadius:2.0]; return; } 
+3
source

I struggled with the same, and it turns out you need to set shadowOpacity to 1.0. In your code, you accidentally use opacity instead of shadowOpacity . This is the same as what I had.

0
source

Besides borderRadius , your shadow layer looks very transparent. Therefore, it will not lose shadow on shadow.superlayer .

-2
source

Source: https://habr.com/ru/post/900987/


All Articles