Set UIImageView in the background programmatically

I have a class without a .xib file, and I want to set the background using UIImageView with the .png file that I have in 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 scaled and loses a lot of quality, instead, in another class with an xib file, I install UIImageView in the background using Interface Builder, and the image is perfect, I want to know how I can install UIImageView in the background, as I I do in the interface builder.

+6
source share
4 answers

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 everyone!

+8
source

Try adding a UIImageView view instead of a 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]; 
+5
source
 UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]]; [self.view addSubview:imgView]; [imgView release]; 

and if your image size is larger than 320x480 pixels, then use the septi answer.

+3
source

Just a small addition to the answers already provided.

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

Source: https://habr.com/ru/post/887178/


All Articles