Wednesday, 19 September 2012

Making apps work on iPhone 5 screen size

Here's a series of gotchas I found in attempting to convert my apps to work correctly on the new taller screen of the iPhone 5.

You need to add a new splash screen at the iPhone 5 size.

This is easy. Just create a new splash screen that's exactly 640x1136 pixels in size.
Call it Default-568h@2x.png and include it in your app.

The existence of this file is the magic key that tells iOS6 that your app is ready for iPhone 5. Without this file your app will always appear bordered.

Use window.rootViewController to add your initial view

First up, if you're building your initial views in code make sure you add your initial view to the window with window.rootViewController rather than the old way of simply adding the view as a subview.
If you don't do this, autorotating will not work at all for your app under iOS 6.

Here's an example:

navigationController = [[UINavigationController alloc] initWithRootViewController: myRootVC];
//[window addSubview:[navigationController view]]; Don't do this!
window.rootViewController = navigationController; // Do this instead!

Use viewDidLayoutSubviews rather than viewDidLoad to set up widget sizes relative to the window

If you lay out your UI in Interface Builder, and set the struts and springs properly, you might expect that inside your viewDidLoad method, your widgets will be auto-laid out to the new taller size.
They won't be! Everything will be exactly the size that you set it to be in Interface Builder at that stage.
As a result you should make sure you don't depend on the size of a widget in the viewDidLoad method. I hit this when I wanted to programatically create a view half the height of the screen.

Of course, by the time the user sees the screen everything is the right size. The issue is that this resizing takes place after the viewDidLoad - but where?

The answer is to use the viewDidLayoutSubviews method which is called after the automatic layout has finished. Override this method and insert your custom sizing.

- (void) viewDidLayoutSubviews
{
    // do clever layout logic here. super call not needed.
}
Automatically loading iPhone 5-sized images where required

You'll be familiar with the way that [UIImage imageNamed:] automatically loads @2x versions of images when running on a retina device.  Unfortunately, imageNamed: will NOT automatically load -586h@2x versions of images when running on an iPhone 5.

Sometimes this doesn't matter, for example icons and non-full screen graphics are probably the same on iPhone 4 & 5. However, if you have full-screen background images, or full-width / height background images for toolbars etc you will have problems. Your 480-high images will most likely get stretched (and will probably look horrid as a result).

You can manually check the screen size and load the right image like this:

UIImage* myImage;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) {
   myImage = [UIImage imageNamed:@"myImage-568h@2x.png"];
} else {
   myImage = [UIImage imageNamed:@"myImage.png"];
}  

Or better you can 'swizzle' the imageNamed function itself, and make it automatically pick up the right image (if available).
Here's an example I found on github to do exactly this (I did not write this code, copyright is with the author).



13 comments:

  1. To get a different sub-view to load when it's inside a controller embedded in a NIB, cause a different NIB to load by overriding the nibName method in your UIViewController subclass, or create a category that does this. A more complete implementation might test for the existence of the NIB first.

    + (BOOL)hasAnamorphicRetina {
    static int isAnamorphic = -1;
    if (isAnamorphic < 0) {
    CGSize size = [ChordCalcController sharedInstance].window.screen.bounds.size;
    CGFloat near = fabs(size.height / size.width - 1.775f);
    isAnamorphic = (near < 0.00001f) ? 1 : 0; // 568/320 == 1.775
    }
    return isAnamorphic == 1;
    }

    - (NSString*)nibName {
    static NSMutableString *name = nil;
    if (name == nil) {
    name = [[NSMutableString stringWithString:[super nibName]] retain];
    if ([[self class] hasAnamorphicRetina]) {
    [name appendString:@"-568h@2x"];
    }
    }
    return name;
    }

    ReplyDelete
  2. That's a good idea. Thanks for the comment!

    ReplyDelete
  3. I fixed some issues on your code. Check it out at http://angelolloqui.com/blog/20-iPhone5-image-loading

    ReplyDelete
  4. Mükemmel bir fikir. Güzel detayları ile kod için teşekkürler. ucuz iphone 4

    ReplyDelete
  5. That's a wise decision really. I found attempting to convert my apps to correctly on the new taller screen of the iPhone 5.

    Custom Apps for iPhone

    ReplyDelete
  6. I have gone through your blog its really giving beneficial information regarding iphone5.
    iPhone 5 Replacement Parts

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete