Piero
Piero

Reputation: 9273

Set UIImageView in background programmatically

i have a class with no .xib file, and i want to set the background with a UIImageView with a .png file i have in the resources, i try this:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.png"]];
[[self view] setBackgroundColor:background];
[background release];

this work, but the image is zoomed in and loses a lot of quality, instead in another class with xib file i set a UIImageView in background by Interface Builder and the image is perfetct, i wanna know how i can set the UIImageView in background as i do in interface builder.

Upvotes: 6

Views: 24600

Answers (4)

tamasgal
tamasgal

Reputation: 26259

Try adding a UIImageView subview instead of UIColor.

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[imageView setImage:[UIImage imageNamed.@"background.png"]];
[self.view addSubview:imageView]; // edited for syntax
[imageView release];

Upvotes: 5

Piero
Piero

Reputation: 9273

Sorry if i forgot to write that my class is a UITableViewController, this is the solution:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[imageView setImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = imageView;
[imageView release];

thanks to all!

Upvotes: 8

visakh7
visakh7

Reputation: 26390

Just a little addition to the answers already provided

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];

Upvotes: 2

saadnib
saadnib

Reputation: 11145

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
[self.view addSubview:imgView];
[imgView release];

and if your image size is grater then 320x480 px then use septi's answer.

Upvotes: 3

Related Questions