Iphone sdk transparent subview background

iphonetransparentuiview

I have a main view with a picture on it.

I am trying to add a subview with [self.view addSubview:view2]; but I want the view2 background to be transparent. Have tried opaque=no and background color to clearcolor and also tried to subclass a uiview and rewrite the drawrect with:

#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

But still doesn't display the background of the subview transparent… any ideas?

Best Answer

Try:

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