iPhone 6 a different storyboard?

2019-02-28 10:30发布

问题:

When we had the iPhone 4 and 5 , we checked the screen size, and made 2 storyboards for each iPhone.

 //iPhone 4
    if (height == 480)
    {
        storyboard = [UIStoryboard storyboardWithName:@"StoryboardiPhone" bundle:nil];
         NSLog(@"Device has a 3.5inch Display.");
    }
    //iPhone 5
    else  if (height == 568)
    {
        storyboard = [UIStoryboard storyboardWithName:@"StoryboardiPhone5" bundle:nil];
          NSLog(@"Device has a 4inch Display.");
    }
    //iPads
    else
    {
        storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
        NSLog(@"Device has a iPad Display  ");

    }

Now there are 2 more iPhones, the question is, is it right to make 5 storyboards for all iPhones and iPad ? seems to me a wrong thing to do, but i couldn't find a way to arrange the views in one device, and make it fit the all the others -and be sure that it always works great.

What will be the right approach right now ?

回答1:

No, you should use AutoLayout and write the appropriate constraints and let the system resize your UI for the various sizes.



回答2:

Best approach is using AutoLayout but if you still have to use different storyboards for different screen size for some reason, following is a working code.

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    //iPad
    storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
}else{
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone){
        // The iOS device = iPhone or iPod Touch
        CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
        if (iOSDeviceScreenSize.height == 480){
            // iPhone 3/4x
            storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone3_4X" bundle:nil];
        }else if (iOSDeviceScreenSize.height == 568){
            // iPhone 5 - 5s - 4 inch
            storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5_5S" bundle:nil];
        }else if (iOSDeviceScreenSize.height == 667){
            // iPhone 6 4.7 inch
            storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone6" bundle:nil];
        } else if (iOSDeviceScreenSize.height == 736){
            // iPhone 6 Plus 5.5 inch
            storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone6Plus" bundle:nil];
        }

    }
}

To enable native iPhone 6 and iPhone 6 plus screen resolution add launch images



回答3:

You should be designing the UI for the wAny/hAny "size class" in Interface Builder. Apply auto layout constraints to describe how the views should adapt to different size classes. If you need to, you can override some constraints for specific size classes.

The code you had before that selected the storyboard to load based on device should be removed. It is no longer needed if you use size classes.

There is an excellent WWDC video that introduces the adaptive UI and size classes. I would recommend watching it.