如何在UITabBarController-> UINavigationController中的ViewController上获取旋转事件?

问题描述:

无论何时用户将iOS设备从纵向旋转到横向,纵向视图是TabBar和NavigationController内的视图,我都想要显示全屏横向视图。如何在UITabBarController-> UINavigationController中的ViewController上获取旋转事件?

然而,willRotateToInterfaceOrientation:时间:不会被调用。我也测试了将ViewController添加为UIDeviceOrientationDidChangeNotification事件的Observer,但是此通知也以未定义的方向调用。

给定任务的最佳和最简单的方法是什么?

也有UIApplicationWillChangeStatusBarOrientationNotificationUIApplicationDidChangeStatusBarOrientationNotification通知。

userInfo字典包含一个封装UIInterfaceOrientation值一个NSNumber对象。使用UIApplicationStatusBarOrientationUserInfoKey访问此值

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(didRotate:) 
               name:UIApplicationDidChangeStatusBarOrientationNotification 
               object:nil]; 

- (void) didRotate:(NSNotification *)notification{ 
    NSNumber *num = [[notification userInfo] objectForKey:@"UIApplicationStatusBarOrientationUserInfoKey"]; 
    NSLog(@"%d", [num intValue]); 
} 
+0

感谢很好的提示。但是,从不调用UIApplicationWillChangeStatusBarOrientationNotification,因为TabBarController不直接响应方向更改(它只允许纵向),因此状态栏永远不会旋转。 – lambruscoAcido