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

夙愿已清 提交于 2019-12-02 00:42:17

问题


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:


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.



来源:https://stackoverflow.com/questions/12753718/dealing-with-different-size-images-in-a-xib-for-iphone5-versus-iphone4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!