2013-03-15 7 views
10

내 UIViewController에 UIView를 추가하고 싶지만 UIView 내부에서 타원형을 만들기로 결정하기 전까지는 투명하게 만들고 싶습니다.UIView 투명하게 만들기

타원을 만들 때까지 내보기의 알파를 0으로 설정할 수 있지만 타원을 추가하려는 경우 배경색은 여전히 ​​검정색입니다.

- (void)drawRect:(CGRect)rect 
{ 
    [self pushContext]; 
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds]; 
    [[UIColor redColor] setFill]; 
    [oval fill]; 
    self.opaque = NO; 
    [self setBackgroundColor:[UIColor clearColor]]; 

    [oval addClip]; 
    [self setBackgroundColor:[UIColor clearColor]]; 

    [self popContext]; 
} 

답변

13
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     self.opaque = NO; 

    } 
    return self; 
} 



- (void)drawRect:(CGRect)rect 
{ 
    [[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0] setFill]; 
    UIRectFill(rect); 
    [self pushContext]; 
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds]; 
    [[UIColor redColor] setFill]; 
    [oval fill]; 

    [oval addClip]; 

    [self popContext]; 
} 
+0

오 흥미로운 솔루션 '예요 불투명 체크 박스를 체크 해제하고 있었다! 고마워. – peter1234

22

하는이 하나

view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0]; 
view.opaque = NO; 
5

를 사용 해보세요 : 여기

는 타원

내 UIView의에서

image

의 무리와 함께 생겼는지 에서 당신의 ViewController, 당신은 불투명도가 NO라고 말할 수 있습니다. 보기의 이름이 myCustomView라고 가정 해 보겠습니다.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.myCustomView.opaque = NO; 
} 

아니면 그냥 스토리 보드로 이동 enter image description here