第一种,固定大小的view,采用CALayer处理阴影问题。
UIView *v= [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
v.backgroundColor = [UIColor whiteColor];
[self.view addSubview:v];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
subView.backgroundColor = [UIColor whiteColor];
subView.layer.cornerRadius = 15.f;
subView.clipsToBounds = YES;
CALayer *layer = [CALayer layer];
layer.frame = subView.frame;
layer.backgroundColor = [UIColor whiteColor].CGColor;
layer.shadowOffset = CGSizeMake(3, 3);
layer.shadowColor = [UIColor lightGrayColor].CGColor;
layer.cornerRadius = 15.f;
layer.shadowOpacity = 0.6;
[v.layer addSublayer:layer];
[v addSubview:subView];
这种方式,经常在处理imageView的圆角和阴影时采用上述的方式。
第二种方式,对UIView单独做处理。
UIView *v=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
v.backgroundColor=[UIColor yellowColor];
//v.layer.masksToBounds=YES;这行去掉
v.layer.cornerRadius=10;
v.layer.shadowColor=[UIColor redColor].CGColor;
v.layer.shadowOffset=CGSizeMake(10, 10);
v.layer.shadowOpacity=0.5;
v.layer.shadowRadius=5;
[self.view addSubview:v];
这种方法一定不能设置view的clipToBounds。