self.bgView.layer.shadowColor = [UIColor orangeColor].CGColor;
// 阴影偏移,默认(0, -3)
// 这个属性和阴影半径配合当其大于阴影半径x设置负值只有左边没有右边,反之亦然,当设置为0时,左右都有;y同理。
self.bgView.layer.shadowOffset = CGSizeMake(-6,4);
// 阴影半径,默认3
self.bgView.layer.shadowRadius = 5;
// 阴影透明度,默认0
self.bgView.layer.shadowOpacity = 0.2;
但是,设置圆角后阴影属性就没有了.
self.bgView.layer.masksToBounds = YES;
self.bgView.layer.cornerRadius = 20;
这时我们可以不在此控件上设置阴影属性,可以加一个控件在其下面设置该控件的阴影属性:如下:
self.bgView.layer.cornerRadius = 5;
self.bgView.layer.masksToBounds = YES;
UIView*belowView = [[UIView alloc]init];
belowView.frame=self.bgView.frame;
// 该颜色一定要设置不然看不到阴影
belowView.backgroundColor= [UIColor whiteColor];
belowView.layer.shadowColor = [UIColor blackColor].CGColor;
belowView.layer.shadowOffset=CGSizeMake(0,0);
belowView.layer.shadowRadius=5;
belowView.layer.shadowOpacity=0.2;
belowView.layer.cornerRadius=5;
[self addSubview:belowView];
[self insertSubview:belowView belowSubview:self.bgView];