Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with different size images in a xib for iPhone5 versus iPhone4?

The previous posting on here about migrating to iPhone 5 only mention adding a new sized launch image* and maybe using AutoLayout if need be.

But I have a few xibs where there is a background image which fills up the whole of the screen (maybe minue the navigation and tab bar, depending upon the view). To support iPhone 5 I will now need different images for the different screen size, how is this dealt with in the xib or elsewhere?

(*incidentally what do you do if the app doesn't use a launch image?)

like image 839
Gruntcakes Avatar asked Jan 25 '26 21:01

Gruntcakes


1 Answers

Here's a link that will actually help you. Save the launch image, iOS doesn't automatically pick the right image if you put in a "[email protected]" at the end of the file name. There are a couple of helper methods mentioned in the above link that will make your job easy.

I have adopted the code in the links I mentioned above and here's the helper function for Obj-C:

-(BOOL) IsTall
{
    return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) && ([[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale] >= 1136);
}

A little category-writing would help too.

@interface NSString (Special)
-(NSString*)correctImageNameForiPhone5
@end

@implementation NSString (Special)
-(NSString*)correctImageNameForiPhone5
{
    if([self isTall])
            return [email protected]_-; //Do the correct NSString magic here
    else
            return -originalImageName-;
}
@end

Finally, when you are accessing the UIImage object:

NSString *filename = @"backgroundImage.png";
UIImage *img = [UIImage imageNamed:[filename correctImageNameForiPhone5]];

The assumption here is that you would have all your iPhone5-specific image file names ending with "-568h@2x". This code sample is definitely not gonna work if you just drop it into your project, but you get the idea. It needs some NSString fixes.

like image 161
Ravi Avatar answered Jan 27 '26 12:01

Ravi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!