Iphone5 iphone5 - iphone4

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

File Name: 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 delay the application on my simulator, the background for iphone5 shows a double size, out of view.

/ * SOLVED THANKS * /

+7
source share
3 answers

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

First: split the .png using the imageNamed: method. Starting with iOS4, you should no longer do this. Next: What are the names of your image? Please note that iPhone5 has a retina display, and your image should be named bg-for5-568h@2x.png , but indicated in the source code as bg-for5-568h .

In addition: in almost every case when your image is not a photograph, what you do is a bad idea. And even if it's a photo, just use the larger image for the iPhone 4 and 4S. This is not much more, so memory is not a problem here! Take a look at the UIImageView contentMode property. You can use this to adjust the position of a larger image. You can also check the UIImageView clipSubviews property to crop the image if it is not full-screen.

Believe me, in my company we had a huge number of hooks for things like ~ ipad, ~ iphone, ~ 2x and even stretchable images. And all these hooks worked perfectly up to date, Apple announced something similar or just a new device. So I decided not to make such hooks anymore. They seem to be very useful in the first place, but the problems you get when there is something new in the market are not worth it!

+5
source

You should add the @ 2x suffix to all retinal images. In your case, your image should be stored as " bg-for5-568h@2x.png ". Hope this solves the problem.

+2
source

I would not advise doing this, what if Apple changes its screen again, and you need to go back and rewrite all your code?

A simple solution is to use:

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

This can lead to some problems with stretching or tiling.

I prefer to use

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

In iOS 6, you can improve this by specifying whether you want to stretch the image or tiles. This allows you to create a border that does not change, and then the center of your image will be partitioned by default and fill the space of your image

+1
source

All Articles