6

再会!

我试图让我的视图(主视图中的视图)变成圆角。我正在这样做,但它不起作用。有任何想法吗?

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;
4

4 回答 4

13

尝试这样的事情:

view.layer.cornerRadius = 5.0;
view.layer.masksToBounds = YES;

对于阴影:

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>

于 2013-02-01T13:44:03.973 回答
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]];
于 2013-12-02T12:00:32.020 回答
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; 
于 2013-02-01T14:12:35.490 回答
0

Stavash的解决方案似乎是正确的,我已经使用过几次了。如果您正在寻找替代方案或坚持使用遮罩层,请参阅此答案:https ://stackoverflow.com/a/13163693/936957

于 2013-02-01T13:55:24.913 回答