Enabling auto layout in iOS 6 while remaining backwards compatible with iOS 5
Asked Answered
R

6

153

What is the best way to take advantage of the new auto layout features of iOS 6 while still providing compability with older devices on earlier versions of iOS?

Royall answered 13/9, 2012 at 17:50 Comment(6)
+1. Where you able to figure this out ? Any clue ?Burrus
@Jennis Not yet. iOS 6 will officially be released tomorrow (9/19/2012). Hopefully that includes some extra documentation on the subject.Royall
I haven't found anything yet. Inquiring minds would like to know!Perilous
I do not think that would be possible. Much like storyboards were not possible on iOS4.Genevagenevan
Outside of providing two nib files, I don't see how that would be possible. But I agree with you.Genevagenevan
Springs and Struts might work for simple layouts #12526519Deth
S
120

Autolayout can be enabled or disabled on each .storyboard or .xib file. Just select the particular file and modify the "Use Autolayout" property using the File inspector in Xcode:

autolayout property in the File inspector

Using autolayout enabled interface files with the deployment target set to an iOS version prior to 6.0 results in compilation errors, e.g.:

Error in MainStoryboard.storyboard:3: Auto Layout on iOS Versions prior to 6.0

One of your options to use autolayout in a project and still preserve compatibility with iOS4-5 is to create two targets: one for deployment target iOS 6.0 and one for an earlier iOS version, e.g.:

enter image description here

You can create two versions for each of your storyboard and XIB files as well and use the autolayout enabled with the 6.0 target and the other with the legacy target, e.g.:

enter image description here

You then add MainStoryBoardAutoSize to the iOS6 target's Build phases and the other file to the iOS4 target. You can learn more about using multiple targets here.

EDIT: As marchinram's answer points out, if you load you storyboard files from code and do not use the "Main Storyboard" setting in Xcode to set the initial storyboard, you can use a single target.

For me, the cost of the added complexity of maintaining multiple targets and interface files seems to outweigh the benefits of using autolayout. Except for a few special cases, you are probably much better to use plain old auto sizing (or layoutSubViews from code) exclusively if iOS4-5 compatibility is required.

Sverre answered 21/9, 2012 at 20:20 Comment(3)
Auto layout requires iOS 6 or later. It does not work with iOS 5! Please verify your claims before posting. iOS 5 simply does not have the required APIs (such as the class NSLayoutConstraint). If you don't believe me, check out what other users experience when they try to use Autolayout with iOS 5: #11252557 #11199481Unaneled
Yes I think you're right I think I heard it in some wwdc videos, but now I tested it on iOS 5.0 device and it crashed. I will go through these videos and check where did I hear it Nonetheless you're right it does crash on iOS 5.0Devilmaycare
@ImreKelényi How would you submit this to the app store? I'm not super familiar with the process, but I thought you submit one zipped archive of your .app file. Does having two targets make that process harder at all? Thanks.Kim
T
47

Do you really need two targets? I got it working like this, I have 2 storyboard like Imre Kelényi said, one with auto layouts enabled and the other without, then in the app delegate i just check which version they are using and select the right storyboard:

#import "AppDelegate.h"

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)

@interface AppDelegate ()
    @property (strong, nonatomic) UIViewController *initialViewController;
@end

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *mainStoryboard = nil;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
        mainStoryboard = [UIStoryboard storyboardWithName:@"iPhone_iOS6" bundle:nil];
    } else {
        mainStoryboard = [UIStoryboard storyboardWithName:@"iPhone_iOS5" bundle:nil];
    }

    self.initialViewController = [mainStoryboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.initialViewController;
    [self.window makeKeyAndVisible];

    return YES;
}

@end

Having 2 targets works aswell but seems like overkill to me

Tareyn answered 25/9, 2012 at 23:40 Comment(4)
You are right. This works as long as you load storyboards from code and do not use use the target's "Main Storyboard" setting in Xcode. I add a reference to your answer from my post.Unaneled
SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO is overkill. Just test an iOS 6-only class against nil. See developer.apple.com/library/mac/#documentation/developertools/…Eclogite
yeah good points, wasn't really think about restoration when I answered initiallyTareyn
Ahem, don't use willFinishLaunchingWithOptions -- I got a "Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named NSLayoutConstraint'" running the simulator under iOS 5.1. The problem does not occur with didFinishLaunchingWithOptions.Ugrian
D
4

If the layout differences are not large, it's a lot easier to use Springs and Struts to position elements.

Deth answered 4/10, 2012 at 18:4 Comment(0)
U
3

Inspired by @marchinram's one target idea, this is the solution I finally came up with. Two storyboards, one for struts-and-springs and one for autolayout. In the target summary, I set the autolayout storyboard as the default. Then, in the appDelegate, I check whether I need to load the pre-6.0 struts-and-springs storyboard after all:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    Class cls = NSClassFromString (@"NSLayoutConstraint");
    if (cls == nil) {
        NSString *mainStoryboardName = nil;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            mainStoryboardName = @"MainStoryboard_iPad_StrutsAndSprings";
        } else {
            mainStoryboardName = @"MainStoryboard_iPhone_StrutsAndSprings";
        }
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:mainStoryboardName bundle:nil];

        UIViewController *initialViewController = [mainStoryboard instantiateInitialViewController];
        self.window.rootViewController = initialViewController;
        [self.window makeKeyAndVisible];
    }

Also, I set the deployment target of the struts-and-springs storyboard to iOS 5.1, and that of the autolayout storyboard to Project SDK(iOS 6.0).

I really wanted to do the switch before the default in storyboard is loaded, in willFinishLaunchingWithOptions: but that results in an 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named NSLayoutConstraint' no matter what I tried.

Ugrian answered 17/1, 2013 at 12:3 Comment(0)
B
2

Try to use RRAutoLayout: https://github.com/RolandasRazma/RRAutoLayout It's iOS6 AutoLayout backport to iOS5.

Buehler answered 13/12, 2012 at 3:4 Comment(0)
M
0

I've found setting the Xibs main view size to Freeform, and then using Autosizing works a treat. No messing about in code for a view issue.

Monique answered 2/1, 2013 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.