根据设备(iPhone或iPad)的不同设备方向

我正在开发一个具有以下要求的Universal项目:

  • 对于iPhone,我只想要纵向。
  • 对于iPad,只有风景。

我如何为iOS 8 (Swift)做到这一点?

根据@ScarletMerlin的建议,在我看来,更改info.plist中的键是我必须满足的要求的最佳解决方案(每种设备的固定方向类型)。

这是我使用的设置的打印屏幕。 也许它可以帮助其他有类似疑问的开发人员。

info.plist文件中的设置

相关源代码如下所示:

UISupportedInterfaceOrientations  UIInterfaceOrientationPortrait  UISupportedInterfaceOrientations~ipad  UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight  

我的建议是检查运行应用程序的硬件。 为此,请使用以下代码行。

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

然后,一旦检测到硬件,使用shouldAutorotateToInterfaceOrientation阻止方向:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { return UIInterfaceOrientationIsLandscape(orientation); } 

举个例子,你可以做到这一点

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) return UIInterfaceOrientationIsLandscape(orientation); // If iPad else return UIInterfaceOrientationIsPortrait(orientation); // Else, it is an iPhone }