2013-02-01 124 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]];