2013-02-01 89 views
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; 
+1

你可以简单地使用层的cornerRadius属性做到这一点。 – Exploring

回答

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>

+0

不工作:(它可以是一些使用ARC – Pavel

+0

我注意到你没有做任何与你分配的UIView - currenView是不会去任何地方 – Stavash

+0

这行后添加view.clipsToBounds = YES – CRDave

0

你的观点有没有大小。它的宽和高为0试着这么做,

currentView = [[UIView alloc] initWithFrame:CGRectMake(0,0 200,200)]; 

,然后应用

currentView.layer.cornerRadius = 8.0; 
currentView.layer.masksToBounds = YES; 
currentView.layer.borderWidth = 1.0; 
+0

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; 如果我喜欢这个角落是圆的,但没有影子。我认为问题出在 view.layer.masksToBounds = NO; – Pavel

+0

为阴影遵循stavash的解决方案 – karim

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