我正在开发一款通用应用程序。现在我想为两种设备设置方向。当应用程序在iPhone上启动时,它以纵向模式打开;当应用程序在iPad上启动时,它以横向模式打开。
这是否可行?
我正在开发一款通用应用程序。现在我想为两种设备设置方向。当应用程序在iPhone上启动时,它以纵向模式打开;当应用程序在iPad上启动时,它以横向模式打开。
这是否可行?
- (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
}
你也可以使用[UIDevice currentDevice].model或[UIDevice currentDevice].systemName来识别设备,然后在shouldAutoRotate方法中根据设备类型返回interfaceOrientation == UIInterfaceOrientationLandscapeLeft(对于iPad)或interfaceOrientation == UIInterfaceOrientationPortrait(对于iPhone)。
您可以通过在应用程序委托中添加一些代码来实现此操作,具体请参见我的答案。
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)
}
}
shouldAutorotateToInterfaceOrientation
方法中使用上述代码来控制每个设备的方向。 - John Parker