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



28 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
  9. Important information it is, People who are planning to repair iPhone 6 screen repair they can come us for best results. .To know about full information then go to iPhone 6 Screen Repairs in London

    ReplyDelete
  10. Nice information.. For more details about this information go to iPhone 5s Screen Repair Services in London

    ReplyDelete
  11. Great information… Here you can get more information by clicking on this link iPhone 6 Screen Repairs in London

    ReplyDelete
  12. I like this information.. If anyone want more information about services they can go through with this Mobile Phone Repairs Manchester

    ReplyDelete
  13. I like this information... If anyone want more information about services they can go through with this iPad Screen Repair London

    ReplyDelete
  14. very nice information about iphone.sydney repair centre provides all kind of services on iphone.iphone 4 screen repair

    ReplyDelete
  15. its a nice post i got some new information from this post.

    iPad screen repair

    ReplyDelete
  16. Tidak hanya akan mengulasan mengenai susunan kartu judi dominoqq online. Anda juga harus mengerti beberapa faktor penentu dalam sebuah permainan
    asikqq
    dewaqq
    sumoqq
    interqq
    pionpoker
    bandar ceme terpercaya
    hobiqq
    paito warna terlengkap
    bocoran sgp

    ReplyDelete

  17. Thinking to upgrade and update your Website & App? Well you deserve what you want, when you pay for it! Therefore InnovationM UK is at your service. Opening doors for advancement to get your milestone easily. InnovationM UK is an app development company in birmingham, which is no.1 in the matter of trust and authenticity among app design companies in london. Whether it's website development in luton, mobile app agency in manchester or app design company in london, we have everything to offer. Looking forward for iphone app development in birmingham, we got your back! InnovationM UK covers you with app design agency in uk as best website design company in luton, mobile app development services in birmingham, mobile app development company in birmingham, mobile development agency in london. As the dgital era is approaching, mobile app development services in birmingham will grow much denser also development of iphone app in derby. There we stay to full-fill, as your trusted mobile agency in uk. So whenever you wish to get your dream app design agency in london, web design company in derby, app design agency in london, Best Software development company in London or app developers sheffield InnovationM UK is right by your side with the best app developers in birmingham. Cheers to your success!

    And also popularly known for app agency oxford, ios app development derby, iphone app development derby, mobile app development derby, app development agency bristol, app development derby development agency brighton, app development cambridge, software development agency manchester, app developers cambridge, android app development derby, app developers oxford, web app agency brighton, app development oxford, software company brighton, software development companies glasgow, UK For More: www.innovationm.co.uk

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

    ReplyDelete
  19. I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. Eren Yeager Coat

    ReplyDelete
  20. The great website and information shared are also very appreciable. johnny knoxville leather jacket

    ReplyDelete
  21. Such an interesting article here.I was searching for something like that for quite a long time and at last I have found it here. Love Hard Leather Jacket

    ReplyDelete
  22. Our the purpose is to share the reviews about the latest Jackets,Coats and Vests also share the related Movies,Gaming, Casual,Faux Leather and Leather materials available. Knight Rider Jacket

    ReplyDelete
  23. The way you describe the topic is amazing and interesting. Thanks for providing this blog. Jimmy Hurdstrom Red Jacket

    ReplyDelete