iPhone / iPad通用应用程序方向问题

4

我正在开发一款通用应用程序。现在我想为两种设备设置方向。当应用程序在iPhone上启动时,它以纵向模式打开;当应用程序在iPad上启动时,它以横向模式打开。

这是否可行?

4个回答

5
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        return YES;
    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
        return YES;
    }

    return NO;
}

2
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  // The device is an iPad running iPhone 3.2 or later.
  // Rotate to landscape
}
else {
  // The device is an iPhone or iPod touch.
  // Rotate to portrait
}

"如何在iOS 3.2中使用UI_USER_INTERFACE_IDOM?"

1
正如@Emil所暗示的那样,您可以在视图控制器的shouldAutorotateToInterfaceOrientation方法中使用上述代码来控制每个设备的方向。 - John Parker

0

你也可以使用[UIDevice currentDevice].model或[UIDevice currentDevice].systemName来识别设备,然后在shouldAutoRotate方法中根据设备类型返回interfaceOrientation == UIInterfaceOrientationLandscapeLeft(对于iPad)或interfaceOrientation == UIInterfaceOrientationPortrait(对于iPhone)。


0

您可以通过在应用程序委托中添加一些代码来实现此操作,具体请参见我的答案

Swift 代码:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue | UIInterfaceOrientationMask.LandscapeRight.rawValue)
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接