简体   繁体   中英

switch background image iphone5 - iphone4

In my FirstViewController I have write this code for switch background if device is iPhone4 or iPhone5:

Filename: bg-for5-568@2x.png
bg-for4@2x.png
bg-for4.png

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *backgroundImage = [[UIImage alloc] init];
    if([[UIScreen mainScreen]bounds].size.height == 568)
    {
       backgroundImage = [UIImage imageNamed:@"bg-for5-568h"];
    }
    else
   {
       backgroundImage = [UIImage imageNamed:@"bg-for4"];
   }
   self.view.backgroundColor = [[[UIColor alloc] initWithPatternImage:backgroundImage] autorelease];
   [backgroundImage release];
}

When i lanch the app on my simulator, the background for iphone5 show double size, out of the view.

/* RESOLVED THANK YOU */

I am not sure if this is the solution for this problem as I am missing some infos, but:

At first: Strip the .png from your imageNamed: method. Since iOS4, you shouldn't do this anymore. The next thing is: What are the Names of your image? Note that an iPhone5 has a retina display, and your image should be named bg-for5-568h@2x.png but referred in the sourcecode as bg-for5-568h .

Besides that: In almost every case where your image isn't a photograph, what you are doing is a bad idea. And even if it is a photograph, simply use the bigger image for the iPhone 4 and 4S as well. It's not that much bigger, so the memory footprint isn't a problem here! Have a look on UIImageView 's contentMode property. You can use this to adjust the position of the larger image. You also might want to check UIImageView s clipSubviews property to clip the image if it isn't fullscreen.

Trust me, in my company we had a loot of hooks for stuff like ~ipad, ~iphone, ~2x and even stretchable images. And all these hooks worked fine till the date, apple announced something similar or simply a new device. So I decided to not do these kind of hooks anymore. They seem to be very helpful in the first place, but the trouble you get when there is something new on the market isn't worth it!

You should append @2x suffix to all of your retina images. In your case your image should be stored as "bg-for5-568h@2x.png". Hope it will resolve the issue.

I would not advise doing this, what if Apple change their screensize again and you have to go back and rewrite all your code?

A simple fix is to use:

  self.view.backgroundColor = [UIColor colorWithPatternImage:/* your image*/];

This could give you some issues with stretching or tiling.

I prefer using

 UIImage* imageName = [[UIImage imageNamed:/*image name*/]resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)];

In iOS 6 you can improve this further by defining if you want the image to stretch or tile. This allows you to create a border which won't change and then the centre of your image by default being tiled and filling the space of your imageview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM