【问题标题】:Different layout for iPad and iPhoneiPad 和 iPhone 的不同布局
【发布时间】:2012-12-26 14:42:47
【问题描述】:

我现在正在制作一个 iphone 应用程序。但将来这个应用程序也应该可用于 iPad。目前我正在使用情节提要,因为以这种方式使其也可用于 iPad 非常容易。

我现在的问题是,一些视图,如长配置文件表单,您将其放在滚动视图中。但是您不能在情节提要中构建滚动视图布局,因此我在代码中创建了它们。但是,如果我希望 iPad 也可以使用此视图布局,我该怎么办?

我应该重写 iPad 的布局代码,然后做一些设备检测吗? 最佳做法是什么?

【问题讨论】:

  • 旁注:我更喜欢使用 UITableViewController 进行注册/登录/配置文件等,因为它需要的代码更少。 UITableViews 真的很强大,iPhone 到 iPad 的适配不需要任何工作。但这是我个人的选择!

标签: iphone objective-c ios ipad storyboard


【解决方案1】:

我认为更好地检测硬件并编写不同的布局代码。
你可以使用苹果的宏:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // Write your layout code for iPad there
}
else
{
     // Write your layout code for iPhone/iPod there
}

【讨论】:

    【解决方案2】:

    以下内容对你有用。

    #define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:@"iPhone"] )
    #define IS_IPOD   ( [[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"] )
    #define IS_IPAD   ( [[[UIDevice currentDevice] model] isEqualToString:@"iPad"] )
    #define IS_IPHONE_5_SCREEN [[UIScreen mainScreen] bounds].size.height >= 568.0f && [[UIScreen mainScreen] bounds].size.height < 1024.0f
    #define IS_IPHONE_4_SCREEN [[UIScreen mainScreen] bounds].size.height >= 480.0f && [[UIScreen mainScreen] bounds].size.height < 568.0f
    
    
    if(IS_IPHONE_5_SCREEN)
    {
        if(IS_IPHONE)
            NSLog(@"Hey, this is an iPhone 5 screen!");
        else if(IS_IPOD)
            NSLog(@"Hey, this is an iPod 5 screen!");
        else
            NSLog(@"Hey, this is a simulator screen with iPhone 5 screen height!");
    }
    else if(IS_IPHONE_4_SCREEN)
    {
        if(IS_IPHONE)
            NSLog(@"Hey, this is a lower iPhone screen than 5!");
        else if(IS_IPOD)
            NSLog(@"Hey, this is a lower iPod screen than 5!");
        else
            NSLog(@"Hey, this is a lower simulator screen than 5!");
    }
    else if(IS_IPAD){
        NSLog(@"Hey, this is an iPad screen!");
    }
    else{
        NSLog(@"Hey, this is an ipad simulator screen!");
    }
    

    干杯!

    【讨论】:

      【解决方案3】:

      解决方案相当简单。 首先,我认为您无法在情节提要中创建滚动视图。但这是可能的! 你可以通过here找到如何做。

      所以现在的解决方案是您专门为 iPad 创建另一个故事板!

      【讨论】:

        猜你喜欢
        • 2014-09-16
        • 2018-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多