简体   繁体   中英

UIView Background Image Transparency Problem

I added a subview with an PNG image that should display with transparency, but I am getting all black where the transparency should be.

The code is:

- (void)viewDidLoad
{
    [super viewDidLoad];
    toolbar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BannerBackground.png"]];
    logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
    logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Logo.png"]];
    [self.view addSubview:logoImage];

}

This is code for a iPad, iOS version 4.3. I have the same problem with both the simulator and the iPad.

Since this is the last view added, I expect it to have az index higher than all of the other views, so the other views should show through the transparent areas. This is not working.

I noticed this behaves differently on iOS 4.3.x than 5.0.x . In 4.3.x I had to set opaque to YES, then set the background image, then set it back to NO.

Old question, but: any chance that your transparency is, for instance, black and not actually transparent? Not that I just did that or anything.

(When drawing the toolbar icon, iOS fills in any pixels, including black ones, with white. Make sure there's actually nothing where you expect your transparent area to be.)

Set your view's property opaque to no. like this [view setOpaque:NO];

Failing setOpaque:NO... check to see if your image is correctly exported and not corrupted etc.. that happened to me once

Create a UIImageView with transparent backgroundColor and your logo as its image, and add that:

UIImageView *logoView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"Logo.png"]];
logoView.backgroundColor = [UIColor clearColor];
[self.view addSubview:logoView];

This should work.

#import <QuartzCore/QuartzCore.h>



logoImage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 56)];
logoImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Logo.png"]];
[logoImage.layer setOpaque:NO];
logoImage.opaque = NO;


[self.view addSubview:logoImage];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM