简体   繁体   中英

i don't get shadow

Here is my code:

CALayer *inLayer =[CALayer layer];
inLayer.backgroundColor = [UIColor redColor].CGColor;
inLayer.frame=CGRectMake(0,0,50, 50);
inLayer.position=CGPointMake(200, 200);
inLayer.cornerRadius=25;
inLayer.masksToBounds=YES;
inLayer.shadowColor=[UIColor blackColor ].CGColor;
inLayer.shadowOffset=CGSizeMake(5,10);
inLayer.shadowOpacity = 0.8;
inLayer.contents=(id)[UIImage imageNamed:@"Close.png"].CGImage;
inLayer.name=@"Close";
[self.view.layer addSublayer:inLayer];

i don't get shadow. something i miss there?

The shadow is drawn outside of the layer's bounds. You have to set inLayer.masksToBounds = NO to see the shadow.

Of course, that will prevent cornerRadius from working.

You need to use two layers: an outer layer that sets the shadow, and an inner sublayer that sets masksToBounds = YES and cornerRadius = 25 .

just remove this line:

inLayer.masksToBounds=YES;

your code works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM