【问题标题】:How to make seperate xib for iphone from already existing one for ipad如何将 iPhone 的 xib 与 ipad 现有的 xib 分开
【发布时间】:2016-12-06 12:31:32
【问题描述】:

我有 iPad 特定的应用程序。我也想在 iPhone 上玩同样的游戏。我的应用程序有 xib,它使用具有许多自动布局约束的大小类“wRegular hRegular”。我想将其转换为 wAny hAny 大小类。 我知道有两种方法可以实现这一点,要么使用自动布局,要么只为 iPhone 创建另一个 xib 文件。 我想使用单独的 xib 文件但是如何在 XCode 7 中制作这些文件? 这将如何运作?

【问题讨论】:

标签: ios xib ios-autolayout


【解决方案1】:

只需创建新的 XIB 文件,命名为您想要的任何名称,然后使用以下代码加载 UIViewController

ViewControllerName *vc = [[ViewControllerName alloc] initWithNibName:@"" bundle:nil];

在 ViewController 中覆盖 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 方法,其中使用以下代码检查设备是 iPhone 或 iPad

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     NSString * nibNameOrNil;
     if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ){
        // iPad
        nibNameOrNil = @“iPad_XIB_NAME”
     } else {
        // iPhone
     }
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self)
     {
        // Custom initialization
     }
     return self;
}

这将根据您的设备类型调用您的 XIB。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 2012-05-14
    • 2012-07-08
    • 2012-06-14
    • 2012-11-09
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多