iphone sdk透明子视图背景

Cat*_*lin 2 iphone transparent uiview

我有一个主视图,上面有一张图片.

我正在尝试添加子视图,[self.view addSubview:view2];但我希望view2背景是透明的.尝试了opaque = no和背景色到clearcolor,并尝试将uiview子类化并用以下内容重写drawrect:

#import "TransparentView.h"


@implementation TransparentView

- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor clearColor]];
        self.opaque=NO;
        self.clearsContextBeforeDrawing=YES;
    }
    return self;
}

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);

    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
}


@end
Run Code Online (Sandbox Code Playgroud)

但仍然没有显示子视图的背景透明......任何想法?

pt2*_*ph8 11

尝试:

view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
Run Code Online (Sandbox Code Playgroud)