Dealing with different size images in a xib for iP

2019-02-24 18:10发布

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?)

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-24 18:51

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 "-568h@2x.png" 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 -imageNameAppendedWith_-568h@2x.png_-; //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.

查看更多
登录 后发表回答