[英]iOS6 Add Shadow to A Container UIView With A Corner Radius
包围的UIView如何同时具有阴影和角半径?
我已经尝试过多次在SO上建议的其他解决方案,但是不幸的是,它们似乎不适用于iOS6(或者至少不适用于我)
所以我想我可以发布此内容,以便找到iOS6解决方案。
我有一个包含两个子视图的UIView
容器
- a custom UIImageView
- a custom UIView
我希望整个UIView的角半径为2.5,但我也希望UIView的阴影为阴影。 但是,到目前为止,我只得到这两个愿望中的一个,从来没有同时出现过。
这是我的代码,我对SO解决方案的尝试不同,因此具有不同的版本,但这只是我的版本之一。
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.cornerRadius = 2.5;
self.layer.masksToBounds = YES;
self.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.1].CGColor; //0.1
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.shadowOpacity = 1.0;
self.layer.shadowRadius = 3.0;
^这里self是包含上述两个子视图的自定义UIView
有人知道iOS6解决此问题的方法吗?
更新
因此,我不需要边框颜色,因此在看到解决方案时并没有添加边框颜色,但是这次我使用以下注释中的解决方案添加了该颜色,似乎UIView越来越圆了,但是我确实希望将合并的UIImageView和UIView取整。
因此,基本上,UIImageView在顶部,UIView在底部。
因此,如何只将UIImageView的顶部四舍五入,而仅将UIView的底部四舍五入。
谢谢。
注意:阴影可以作为一个整体起作用,但是角半径不能作为一个整体起作用?
我想到了。
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8].CGColor;
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.layer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
self.layer.shadowOpacity = 1.0;
self.layer.shadowRadius = 3.0;
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[self addSubview:container];
[container addSubview:self.someCustomUIView];
[container addSubview:self.someCustomImageView];
container.layer.cornerRadius = 2.5;
container.layer.masksToBounds = YES;
所以基本上:
我认为您应该更改以下代码行:
self.layer.masksToBounds = YES;
到这个
self.layer.masksToBounds = NO;
如果将masksToBounds设置为YES,那么您将看不到任何超出视图范围的东西,阴影就是这种情况。
这段代码来自我当前的项目(iOS 6),工作正常。 我可以看到圆角和阴影。
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 5.0;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0, -1);
self.layer.shadowOpacity = 0.6;
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect: self.layer.bounds];
self.layer.shadowPath = shadowPath.CGPath;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.