1

当我们拥有 时iPhone 4 and 5,我们检查了屏幕尺寸,并为每部 iPhone 制作了 2 个故事板。

 //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  ");

    }

现在还有 2 部 iPhone,问题是,为所有 iPhone 和 iPad制造5部是否正确? storyboards在我看来这是一件错误的事情,但我找不到一种方法来安排一个设备中的视图,并使其适合所有其他设备 - 并确保它总是很好用。

现在正确的方法是什么?

4

3 回答 3

5

最好的方法是使用 AutoLayout,但如果由于某种原因您仍然必须为不同的屏幕尺寸使用不同的故事板,以下是一个工作代码。

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];
        }

    }
}

要启用原生 iPhone 6 和 iPhone 6 plus 屏幕分辨率,请添加启动图像

在此处输入图像描述

于 2014-09-28T12:31:06.530 回答
2

不,您应该使用AutoLayout并编写适当的约束,并让系统调整您的 UI 以适应各种尺寸。

于 2014-09-15T19:12:24.927 回答
1

您应该在 Interface Builder 中为 wAny/hAny“大小类”设计 UI。应用自动布局约束来描述视图应如何适应不同的大小类。如果需要,您可以覆盖特定尺寸类的一些约束。

您之前根据设备选择要加载的情节提要的代码应该被删除。如果您使用尺寸等级,则不再需要它。

有一个优秀的 WWDC 视频介绍了自适应 UI 和尺寸类。我会推荐观看它。

于 2014-09-24T01:10:06.210 回答