xcode 为 iPad 或 iPhone 加载单独的 XIB 时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5306346/
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
Trouble with loading a separate XIB for iPad or iPhone
提问by Linuxmint
Im having trouble figuring out how to load a separate XIB for iPad or for iPhone in a Universal app.
我无法弄清楚如何在通用应用程序中为 iPad 或 iPhone 加载单独的 XIB。
Its easy enough to convert the Xcode project to Universal and have separate Main view interfaces.
将 Xcode 项目转换为 Universal 并具有单独的主视图界面非常容易。
My problem is the secondary view and getting it to have separate interfaces for both iPad and iPhone.
我的问题是辅助视图并让它为 iPad 和 iPhone 提供单独的界面。
Please offer any help you can give as I have been working on this issue for days without success. Thanks in advance!
请提供您可以提供的任何帮助,因为我已经在这个问题上工作了好几天没有成功。提前致谢!
回答by Kenny Wyland
I know that @indragie's answer is a very common one, but it's a common misunderstanding that will cause you a lot more work than you actually need to do.
我知道@indragie 的回答很常见,但这是一种常见的误解,会导致您做的工作比实际需要做的多得多。
As long as you name the xib files a certain way, they will be automatically selected for either iPhone or iPad. Check out my answer to this same problem on another post:
只要您以某种方式命名 xib 文件,它们就会自动为 iPhone 或 iPad 选择。在另一篇文章中查看我对同一问题的回答:
回答by Digdas
Just add a file named MainView~ipad.xib makes it load this one i.s.o. MainView.xib when running on a iPad..
只需添加一个名为 MainView~ipad.xib 的文件,它就会在 iPad 上运行时加载这个 iso MainView.xib ..
回答by indragie
You can use UI_USER_INTERFACE_IDIOM()
to check whether the application is running on an iPad or an iPhone/iPod touch:
您可以使用UI_USER_INTERFACE_IDIOM()
来检查应用程序是在 iPad 还是 iPhone/iPod touch 上运行:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// Running on iPad
} else {
// Running on iPhone or iPod touch
}
For more information on loading NIB files, read Apple's Resource Programming Guideon Nib Files. You may want to specifically check out the Loading Nib Files Programmaticallysection which shows how to programatiically load a NIB from within your code using NSBundle
. You can then use this in conjunction with the above code to correctly load the proper NIB depending on which device you're runining on.
有关加载 NIB 文件的更多信息,请阅读 Apple 的Nib 文件资源编程指南。您可能需要专门查看以编程方式加载 Nib 文件部分,该部分展示了如何使用NSBundle
. 然后,您可以将其与上述代码结合使用,以根据您运行的设备正确加载正确的 NIB。