我通过拥有一个根视图控制器(这可能是一个 UITabBarController)并在它的 viewDidLoad 方法中订阅旋转事件来做到这一点:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
然后在 didRotate: 方法中,我查看旋转发生时哪个视图控制器可见,以及手机的方向:
- (void) didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
/*
DEVICE JUST ROTATED TO PORTRAIT MODE
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown
*/
if(orientation == UIDeviceOrientationPortrait) {
/*
DEVICE JUST ROTATED TO LANDSCAPE MODE
*/
}else if(orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) {
}
}
在那个 didRotate: 中,您可以查看哪个是可见的 viewController 并从那里做您想做的事情。
当特定视图控制器可见并且手机旋转为横向时,我会在横向模式下呈现模态视图控制器。如果任何其他视图控制器可见,我将忽略该事件。
我强制我的模态视图控制器在其 viewWillAppear 方法中以横向模式显示 - 如果他们想要,我可以给任何人这个代码。
希望这可以帮助。
戴夫