UIView with rounded corners and drop shadow?

前端 未结 30 3077
一整个雨季
一整个雨季 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:15

    I write this UIView category method to solve this problem, uses separate views for the shadow and the corner radius.

    -(UIView *)shadowedWrapViewWithBounds:(CGRect)bounds {
    UIView *baseView = [[UIView alloc] init];
    baseView.bounds = bounds;
    baseView.backgroundColor = [UIColor clearColor];
    baseView.layer.shadowColor = [UIColor blackColor].CGColor;
    baseView.layer.shadowOffset = CGSizeMake(0, 0);
    baseView.layer.shadowOpacity = 0.7;
    baseView.layer.shadowRadius = 4.0;
    
    // improve performance
    baseView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:baseView.bounds cornerRadius:4].CGPath;
    baseView.layer.shouldRasterize = YES;
    baseView.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    [baseView addSubview:self];
    //use Masonry autolayout, self can set corner radius
    [self makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(baseView);
    }];
    
    return baseView;
    }
    

提交回复
热议问题