xcode 在 Interface Builder 中为通用应用程序创建单个 .xib?(iOS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10029332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:52:56  来源:igfitidea点击:

Create single .xib for Universal app in Interface Builder? (iOS)

objective-ciosxcodeinterface-builderuniversal

提问by WendiKidd

Apologies if this is a silly question, but I've done some googling and searched SO and haven't found anyone asking this exact question.

抱歉,如果这是一个愚蠢的问题,但我已经做了一些谷歌搜索和搜索,但没有发现有人问这个确切的问题。

I have been doing iOS development for some time now, but I am completely new to the Interface Builder. What I want to know is this: is there any way to just create ONE .xib file and then use it for both iPhone and iPad in a Universal application?

我从事 iOS 开发已有一段时间了,但我对 Interface Builder 完全陌生。我想知道的是:有没有办法只创建一个 .xib 文件,然后在通用应用程序中将它用于 iPhone 和 iPad?

It seems silly to me to have to create them separately; why do twice the work laying something out more than once in Interface Builder when I could do it once (with minor adjustments for screen size) in code?

必须分开创建它们对我来说似乎很愚蠢;当我可以在代码中完成一次(对屏幕大小进行细微调整)时,为什么要在 Interface Builder 中多次布置一些东西?

Please let me know if I'm missing/misunderstanding something here. Like I said, I'm a complete Interface Builder newbie :)

如果我在这里遗漏/误解了什么,请告诉我。就像我说的,我是一个完整的 Interface Builder 新手 :)

EDIT: I have submitted non-interface-builder games to the App Store in the past where the iPhone and iPad versions were identical, so I'm not concerned with making the game look/feel different on each device. I intend for them to look exactly the same, aside from some slight positioning changes due to the difference in aspect ratio.

编辑:我过去曾向 App Store 提交非界面构建器游戏,iPhone 和 iPad 版本相同,所以我不关心让游戏在每个设备上的外观/感觉不同。我想让它们看起来完全一样,除了由于纵横比的差异而导致的一些轻微的定位变化。

回答by themarketka

If you know what the resulting view would look like, based on autoresizing, you can indeed use only one .xib. May come in handy if the view is just some sort of a shared component that autoresizes as you want it to. However, if you need the view to look way different on iPad than on iPhone, just use two .xibs. It's possible then to load the appropriate one as needed, for example in instance initializer, like this controller's -init:

如果您知道基于自动调整大小的结果视图会是什么样子,您确实可以只使用一个.xib. 如果视图只是某种可以根据需要自动调整大小的共享组件,则可能会派上用场。但是,如果您需要视图在 iPad 上与 iPhone 上的外观不同,只需使用两个.xibs。然后可以根据需要加载适当的一个,例如在实例初始化器中,就像这个控制器的 -init:

- (id)init
{
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
         self = [super initWithNibName:@"YourNibForPad" bundle:nil];
    }
    else
    {
         self = [super initWithNibName:@"YourNibForPhone" bundle:nil];
    }
    if (self) { /* initialize other ivars */ }
    return self;
}

回答by WendiKidd

The main reason that XIBs are separate files is because Apple feel that UIs designed for iPhones/iPod touches and iPads should be tailored to each respectively. This is echoed in their their iOS App Programming Guide, which says the following:

XIB 是单独文件的主要原因是因为 Apple 认为为 iPhone/iPod touch 和 iPad 设计的 UI 应该分别为每个人量身定制。这在他们的iOS App Programming Guide 中得到了回应,其中说明如下:

For views, the main modification is to redesign your view layouts to support the larger screen. Simply scaling existing views may work but often does not yield the best results. Your new interface should make use of the available space and take advantage of new interface elements where appropriate. Doing so is more likely to result in an interface that feels more natural to the user—and not just an iPhone app on a larger screen.

对于视图,主要修改是重新设计视图布局以支持更大的屏幕。简单地缩放现有视图可能有效,但通常不会产生最佳结果。您的新界面应该利用可用空间并在适当的地方利用新的界面元素。这样做更有可能让用户感觉更自然的界面——而不仅仅是大屏幕上的 iPhone 应用程序。

Whilst it can take time to maintain two XIBs for what is effectively one UI, I feel it is more straightforward than using one XIB and then having to connect up most of your UI elements in order to move them around programmatically when that XIB loads. After all, with two XIBs at least you can seewhat each UI looks like, and then make visual changes easily.

虽然为有效的一个 UI 维护两个 XIB 可能需要时间,但我觉得这比使用一个 XIB 然后必须连接大部分 UI 元素以便在加载该 XIB 时以编程方式移动它们更简单。毕竟,至少有两个 XIB,您可以看到每个 UI 的样子,然后轻松进行视觉更改。

As an aside, don't forget iOS 5's Storyboards (read about them here), which make managing a view/view controller hierarchy much simpler.

顺便说一句,不要忘记 iOS 5 的 Storyboards(在这里阅读它们),它使管理视图/视图控制器层次结构变得更加简单。

回答by Pagly

Try to name them

尝试命名它们

MyCell.xiband MyCell ~ ipad.xib

MyCell.xibMyCell ~ ipad.xib

then:

然后:

[self.tableView registerNib: @"MyCell" forCellReuseIdentifier: @"MyUniqueIdentifier"];

回答by kj13ennett

If your using IB, you need to create 2 separate xib files for iPhone and iPad. You need a separate iPad xib to make your app comply with the Apple iPad UI guidelines.

如果您使用 IB,则需要为 iPhone 和 iPad 创建 2 个单独的 xib 文件。您需要一个单独的 iPad xib 来使您的应用符合 Apple iPad UI 指南。