美好的一天!
我试图让我的视图(在主视图中查看)制作圆角。我这样做,但它不起作用。有任何想法吗?
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
currenView = [[UIView alloc] init];
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:currenView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(30.0, 30.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = currenView.bounds;
maskLayer.path = maskPath.CGPath;
currenView.layer.mask = maskLayer;
}
return self;
答案 0 :(得分:13)
尝试这样的事情:
view.layer.cornerRadius = 5.0;
view.layer.masksToBounds = YES;
for shadow:
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
view.layer.masksToBounds = NO;
view.layer.shadowRadius = 5.0f;
确保导入<QuartzCore/QuartzCore.h>
答案 1 :(得分:2)
这是代码。 Alloc初始化视图并发送到此方法以获得四舍五入。你可以选择围绕你想要的任何角落。同时给出阴影笔触颜色。
-(void) setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners withColor: (UIColor*) color
{
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(9.0, 9.0)];
CAShapeLayer* shape = [[[CAShapeLayer alloc] init] autorelease];
[shape setPath:rounded.CGPath];
shape.strokeColor = [[UIColor grayColor] CGColor];
view.backgroundColor=color;
view.layer.mask = shape;
}
像这样调用方法。
[self setMaskTo:ABCView byRoundingCorners:UIRectCornerAllCorners withColor:[UIColor greenColor]];
答案 2 :(得分:0)
Stavash的解决方案似乎是正确的,我已多次使用它。如果您正在寻找替代方案或坚持使用masklayers,请参阅以下答案:https://stackoverflow.com/a/13163693/936957
答案 3 :(得分:0)
您的观点没有大小。它的w和h是0.尝试类似的东西,
currentView = [[UIView alloc] initWithFrame:CGRectMake(0,0 200,200)];
然后申请
currentView.layer.cornerRadius = 8.0;
currentView.layer.masksToBounds = YES;
currentView.layer.borderWidth = 1.0;