iOS – 无需使用笔尖即可支持iPad和iPhone

我试图编写一个应用程序,而不使用笔尖 ,我会以编程方式做的一切。

现在的问题是,我将如何支持iPadiPhone ? 显然,我不能这样做

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { // load iPad nib } else { // load iPhone nib } 

如果我创build2 ViewControllers ,那么IBAction将是多余的。

任何build议?

您可能应该只是找出applicationDidFinishLaunching的设备types,然后为每个设备加载单独的控制器。 但是,如果您只想为所有设备实现一个实现,请执行以下检查:

  if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) { //do some iPad stuff } else { CGFloat screenH = [UIScreen mainScreen].bounds.size.height; if([UIScreen mainScreen].scale == 2.f && screenH == 568.0f) { //do iPhone 5 stuff } else { //do iPhone 4S and iPhone 4 stuff //the dimensions are the same however, if you want to do iPhone 4S specific stuff // you'll need to do additional checks against video resolution or other differences etc } } 
 CGFloat height = [UIscreen mainScreen].bounds.size.height; if(height==568.00 || height == 480.0) { //For iphone 5 and iphone 4 } else { //For ipad } 

你可以在你的AppDelegate中使用这个代码

 - (BOOL) isPad() { if(UI_USER_INTERFACE_IDIOM) { return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad); } else { return NO; } } 

此链接提供了一些有关成语的信息http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/macro/UI_USER_INTERFACE_IDIOM

要么

您可以检查屏幕的高度和宽度,以了解其是iPhone还是iPad

 CGRect screen = [[UIScreen mainScreen] bounds]; CGFloat width = CGRectGetWidth(screen); CGFloat height = CGRectGetHeight(screen); 

如果你不使用任何笔尖,一切都编程,你不想创build两个视图控制器的iPhone和iPad。 请记住,不要依赖任何静态值。 即你的计算应根据self.view.bounds事情(我的意思是创build意见/子视图)。 然后,如果一些特定的function,只支持在iPad做检查

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 

一个视图控制器为你完成了所有的工作。