我正在进行一个通用项目,需要满足以下要求:
- 对于iPhone,只允许纵向方向。
- 对于iPad,只允许横向方向。
如何在(Swift)中实现?
我正在进行一个通用项目,需要满足以下要求:
如何在(Swift)中实现?
根据@ScarletMerlin的建议,我认为更改info.plist中的键是我需要满足要求的最佳解决方案(每种设备都有固定的方向类型)。
这是我使用的设置截图。也许它可以帮助其他有类似疑问的开发者。
相关源代码如下:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
我的建议是检查您的应用程序运行的硬件。为此,请使用以下代码行。
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
}