UIView with rounded corners and drop shadow?

前端 未结 30 2612
一整个雨季
一整个雨季 2020-11-22 08:00

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.

30条回答
  •  一向
    一向 (楼主)
    2020-11-22 08:16

    This worked for me. Trick was to move the background color from the main view to the layer.

    CALayer *layer = view.layer;
    layer.cornerRadius = 15.0f;
    layer.masksToBounds = NO;
    
    layer.shadowOffset = CGSizeMake(0, 3);
    layer.shadowColor = [[UIColor blackColor] CGColor];
    layer.shadowRadius = 2.0f;
    layer.shadowOpacity = 0.35f;
    layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:layer.bounds cornerRadius:layer.cornerRadius] CGPath];
    
    CGColorRef  bColor = view.backgroundColor.CGColor;
    view.backgroundColor = nil;
    layer.backgroundColor =  bColor ;
    

提交回复
热议问题