Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chen8002004

macrumors newbie
Original poster
May 22, 2011
26
0
I am trying to add a stretchable image as background in UIView with the following code. However, the image is shown one after another, but not stretched. Can anyone help me about it?

Code:
UIImage *theImage = [[UIImage imageNamed:@"BackgroundImage.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
    [self setBackgroundColor:[UIColor colorWithPatternImage: theImage]];
 
Last edited by a moderator:
Hi robbieduncan,
You are correct. My code is not the correct way to solve this problem. So what would you suggest to solve this problem? I can think about two ways to solve this problem:
1. Create an Image view and stretch the image
2. Use a very large image to set as background
 
I finally find a solution by modifying the root layer to display a stretched image as background. Any other better solutions?

Code:
	UIImage *theImage=[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BackgroundImage" ofType:@"png"]];
	self.layer.contents=(id)[theImage CGImage];
	self.layer.contentsGravity=kCAGravityTop;
	[theImage release];
	self.contentMode = UIViewContentModeScaleToFill;
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.