Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - UIView's backgroundColor - using a png with transparency?

I'm setting the background color of my UIView (which is within a UIViewController) as follows:

myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myTransparentBG.png"]];

I've even tried messing with [myView setOpaque:NO]; but the image doesn't appear transparent. It has a black background to it. Am I abel to fix this programmatically? Otherwise, how are we supposed to set a transparent background to our view?

This seems like a question that should have been asked before, but I couldn't find an answer.

like image 562
RyanJM Avatar asked Oct 30 '10 16:10

RyanJM


People also ask

Do transparent Pngs work on iPhone?

Images with a transparent background (i.e., a transparent . png) can be uploaded to be used as a logo or a sticker on your video.

Does PNG support background transparency?

The GIF and PNG formats also both support transparency. If you need any level of transparency in your image, you must use either a GIF or a PNG. GIF images (and also PNG) support 1-color transparency. This basically means that you can save your image with a transparent background.


1 Answers

When using a transparent pattern, after setting the backgroundColor property, you need to set opaque property to NO on both view and its layer. Here's a sample:

UIView* paperView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,700)];
paperView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"paper.png"]];

[paperView.layer setOpaque:NO];
paperView.opaque = NO;
like image 117
esad Avatar answered Oct 02 '22 18:10

esad