0

I have a background image covering the entire screen in my app, with a little bit extra on the sides for a parallax effect. Since i support older phones, i need to have logic that checks size bounds to select the "short" or "long" image (4 -> 5). Now with the new phones i need even more logic.

I have made a method that works, but it's not exactly "adaptive"... i am wondering if i have missed something that might bite me. (apart from that i obviously have to revisit this when there are new resolutions coming out...)

App doesn't support landscape mode right now. I have a "regular" image to cater for 3gs/4/4s with @2x that is the default setting, and in ViewDiwLoad of my rootcontroller i run this code, which i've tested and it works:

    if (screenBounds.size.height == 568) {
    backg.image = [UIImage imageNamed:@"background660x1156.png"];
}else if (screenBounds.size.height == 667) {
    backg.image = [UIImage imageNamed:@"background770x1334.png"];
}else if (screenBounds.size.height > 667) {
    backg.image = [UIImage imageNamed:@"background1262x2228.png"];
}

Should i do this in a different way?

Mathias
  • 3,879
  • 5
  • 36
  • 48
  • Could you explain what's not adaptive about it? In the last sentence you state that 'it works' :)? – Hless Sep 23 '14 at 09:19
  • Well, heh, maybe that i've hardcoded the image choice based on pixel sizes. In a way i guess it does adapt, so... :) I am basically just wondering if someone has a smarter way to do it – Mathias Sep 23 '14 at 11:34

1 Answers1

1

I suggest you look at size classes. Particularly, watch the Building Adaptive Apps with UIKit (Session 216) video from WWDC 2014.

Instead of hard coding your sizes, you can use the image asset catalog to target various sizes. To get started open an image in the asset catalog and in the inspector on the right, set a value for Width/Height. You can specify a specific image for Any/Compact/Regular sizes.

size classes in image assets

For example, you could set your background660x1156.png for width: Any, and height: Any & Compact. That would then apply to any iPhone target in portrait.

If you have to target older iOS versions, then this may not help you, but it is the Apple recommended solution moving forward. Read the asset catalog help for more advice.

squarefrog
  • 4,750
  • 4
  • 35
  • 64
  • Hey man thanks for your input! As you probably have guessed, i need to support older versions of IOS... so this won't do me much good - i can't even use autolayout! :( Thanks for trying though. – Mathias Sep 25 '14 at 11:21
  • In that case I think the solution you already have may be your only option. – squarefrog Sep 25 '14 at 11:49