PDA

View Full Version : change background image in self.view




tawpie
Aug 3, 2008, 11:11 PM
ok... I put a background image on my app, sort of like wallpaper. It's pretty easy to do, I start with a UIImageView, set the background image, add some labels and buttons, then copy it to self.view. My view is created programmatically... IB wouldn't do what I wanted so the view creation activity occurs in the loadView method in my mainViewController.

Now, I want to change the background image only, not the labels, not the buttons BUT since self.view is evidently not a UIImageView, one cannot set the background image.

I tried a mutable copy of self.view to a "new" UIImageView, that blows up at the mutable copy.

I can do it via subviews but that seems like a longer way around since I get to create my buttons and labels for each different wallpaper.

I haven't been able to locate much on "self.view" in the documentation, it started showing up in examples and obviously belongs to the view controller but what is it and where did it inherit from?

Suggestions are highly appreciated!



macfanboy
Aug 3, 2008, 11:35 PM
is this what you need?


imageView.image = [UIImage imageNamed:@"picture.png"];


hope it works

Sbrocket
Aug 4, 2008, 02:13 AM
As long as you actually did what you described and set self.view to the UIImageView you created, then it is an instance of UIImageView. The problem you're probably running into is the the view property of a UIViewController is defined more generally as a UIView, so you need to make sure the compiler knows that it is a UIImageView and doesn't complain with an error.


((UIImageView*)self.view).image = [UIImage imageNamed:@"myNewImage.png"];

tawpie
Aug 4, 2008, 11:24 AM
wow -- that was exactly the solution. Thank you Sbrocket.

The compiler certainly behaved as if the image property wasn't "there", which led me to wonder what it actually thought the view property was... but I had no idea you could "tell" the compiler that self.view is really a UIImageView.

Again thanks!