Reputation: 1899
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?
Upvotes: 2
Views: 10617
Reputation: 11880
I've had some cases where ... addSubview:clearView]
seemed to reset the background color of clearView
(WTF!) to something not clear. I added a
[clearView setBackgroundColor:nil];
somewhere after that and it seemed to help.
Upvotes: 0
Reputation: 1114
In the function
- (void)drawRect:(CGRect)rect
try to update
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
to
const CGFloat BACKGROUND_OPACITY = 0.85; //Note: update this value to what you need
CGContextSetRGBFillColor(context, 1, 1, 1, BACKGROUND_OPACITY); // You can change 1,1,1 to the needed values
This link might help you http://www.cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
Upvotes: 1
Reputation: 15597
Is the view being loaded from a nib file? If so, the -initWithFrame:
won't be called; -initWithCoder:
will be called instead. A better place to do this initialization might be in -viewDidLoad
. But setting the background color to [UIColor clearColor]
should definitely do the trick.
Upvotes: 4
Reputation: 10245
Try:
view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
Upvotes: 11
Reputation: 1907
Try coloring the subview's background with a 0.0 for Alpha. That should make it completely transparent.
Something like this:
UIColor *myUIColor = [UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha:0.0];
Upvotes: 2