Reputation: 1051
I am a hobbyist iOS developer (quite a beginner actually). I am currently making an iPhone app and trying to define a universal background image that would work on 480, 960, and the 1136 resolutions.
My idea is to put an imageview into the background with the image, and all other views on top of it would use clearColor as background; so, the background image would always be visible. The problem is that the image cannot get distorted; so, I actually have three different images to display on the various screen sizes.
The problem is that I don't really know how to go make this concept work. I tried to search for an answer on the Internet but to no avail, which made me think that there might be a better way to do this.
Can someone get me started with the above idea or show me a way to achieve the same thing but more efficiently?
Upvotes: 3
Views: 4870
Reputation: 3738
For 480px - ImageName.png
For 960px - [email protected] (It will be automatically taken as image for retina display).
For 1136px, please refer this link.
Upvotes: 1
Reputation: 1051
Okay I believe I solved it (with the help of the bellow commenters :). Here is my code that seems to work (although it might not be the best solution; so, if you have a better way please let me know):
UIImage *backgroundImage = [[UIImage alloc] init];
if ([UIScreen mainScreen].bounds.size.height > 480.0f) {
backgroundImage = [UIImage imageNamed:@"background-568h.png"];
}
else {
backgroundImage = [UIImage imageNamed:@"background.png"];
}
self.window.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
One important thing: even though there is no device that currently uses a four inch non-retina display, you need to use a non-retina version when you refer to the picture in your code and then include a retina version with the smaller image. So need to have a [email protected] for the referred background-568h.png in the code. Doesn't make sense, but that's the way it works... :P
Upvotes: 4
Reputation: 14068
It is rather common and recommendable to provide individual images for each resolution. However, you idea is quite understandable. I assume that your graphic scales nicely into all the different aspect ratios of the various devices. If so then simply create an image (png I suppose) with the largest resolution that you want to use. Assign this image to the UIImageView object(s) that you are using for the background. Set the content mode to "Scale to fill" in IB or UIViewContentModeScaleToFill programmatically.
Upvotes: 1