UIView相容陰影和圓角
阿新 • • 發佈:2018-12-05
由於設定陰影需要masksToBounds設定為NO,而設定圓角需要masksToBounds為YES,因此相互矛盾,不能用一般法解決。
設原來要顯示的view為exhibitView
1.需要將當前要展示的UIView加在一個新的UIView,暫且叫做shadowView,將其設定陰影
初始化
- (UIView *)shadowView{
if (_shadowViewForHeadPicture == nil) {
//shadowView
_shadowView = [[UIView alloc]init];
//不設定frame的話,不能子檢視的點選事件,但也可以正常顯示檢視,另外設定的frame一定要將子檢視包含在內
_shadowView = CGRectMake(0, 0, WIDTH, HEIGHT/2);
_shadowView = [UIColor colorWithRed:203.0/255 green:231.0/255 blue:247.0/255 alpha:1].CGColor;
_shadowView.layer.masksToBounds= NO;//預設是NO
_shadowView.layer.shadowOffset = CGSizeMake(0, 5);
_shadowView.layer.shadowRadius = 20 ;
_shadowView.layer .shadowOpacity = 1;
_shadowView.layer.shouldRasterize = YES;
}
return _shadowView;
}
將它加在父檢視上
[self addSubview:self.shadowView];
然後再用這個_shadowView新增本來要顯示的檢視
[self.shadowView addSubview:exhibitView];
2.在要展示的exhibitView再設定圓角,並讓
exhibitView.layer.masksToBounds= YES;
效果可參考下圖白色的圓形View