【发布时间】:2011-02-23 17:41:44
【问题描述】:
我正在开发通用应用程序。现在我想为这两种方式设置方向,当应用程序在 iPhone 上启动时,它以纵向模式打开,当应用程序在 iPad 上启动时,它以横向模式打开。
有可能吗?
【问题讨论】:
标签: iphone ipad orientation
我正在开发通用应用程序。现在我想为这两种方式设置方向,当应用程序在 iPhone 上启动时,它以纵向模式打开,当应用程序在 iPad 上启动时,它以横向模式打开。
有可能吗?
【问题讨论】:
标签: iphone ipad orientation
- (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;
}
【讨论】:
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
}
“How does one get UI_USER_INTERFACE_IDOM to work with iOS 3.2?”
【讨论】:
shouldAutorotateToInterfaceOrientation 方法来控制每个设备的方向。
您也可以使用 [UIDevice currentDevice].model 或 [UIDevice currentDevice].systemName 来识别设备,然后在 shouldAutoRotate 方法中根据设备类型返回 interfaceOrientation ==UIInterfaceOrientationLandscapeLeft 用于 ipad 和 interfaceOrientation ==UIInterfaceOrientationPortrait 用于 iphone。
【讨论】:
您可以通过根据my answer here 向您的应用委托添加一些代码来做到这一点。
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)
}
}
【讨论】: