Frage

I actually have something simple but I just can't work it out as I'm fairly new to the iOS SDK. So I have a backgroundcolor on an UIView. On top of that I would like to place an image covering the whole UIView as a kind of backgroundimage. The image I'm using is a png which contains transparent areas. I'd like to see the background color through this transparent areas. I do not want to set the transparency of the image itself.

War es hilfreich?

Lösung

I believe you can set the background color of your view to an image, but this solution should help you learn more about views:

From code (assuming you've added your image to your project):

UIImage *image = [UIImage imageNamed:@"imageFileName"];
UIImageView *imageView = [[UIImageView alloc] initWIthImage:image];
[backgroundView addSubview:imageView];

On storyboard, drag an image view onto your background view. You can configure the image view's frame and image in storyboard. Once you drag the image view into your scene pay attention to where it lands in the view hierarchy. You can choose how exactly you want to set this up, for example, do you want the image view to be a sibling of the background view and lay on top of it? or do you want the image view to be a child of the background view and take up the whole background view's bounds? Same thing as far as the user is concerned, just different organization under the hood.

Andere Tipps

If you create a transparent area in an image in ex Photoshop, and saving as PNG it will be transparent in Xcode. That's why I always use PNG images.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top