iphone sdk transparent subview background

10,605

Solution 1

Try:

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

Solution 2

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.

Solution 3

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];

Solution 4

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

Share:
10,605
Catalin
Author by

Catalin

A software mobile developer enthusiast...

Updated on June 04, 2022

Comments

  • Catalin
    Catalin almost 2 years

    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?