如果您打算为所有视图控制器启用或禁用旋转,则不需要继承 UINavigationController。而是使用:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
在您的 AppDelegate 中。
如果您计划支持应用程序中的所有方向,但父视图控制器(例如 UINavigationController 堆栈)上的不同方向,您应该使用
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
从 AppDelegate 结合您的父视图控制器中的以下方法。
- (BOOL)shouldAutorotate
和
- (NSUInteger)supportedInterfaceOrientations
但是,如果您计划在同一个导航堆栈中的不同 CHILDREN ViewControllers 中有不同的方向设置(像我一样),您需要检查导航堆栈中的当前 ViewController。
我在 UINavigationController 子类中创建了以下内容:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
int interfaceOrientation = 0;
if (self.viewControllers.count > 0)
{
id viewController;
DLog(@"%@", self.viewControllers);
for (viewController in self.viewControllers)
{
if ([viewController isKindOfClass:([InitialUseViewController class])])
{
interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
else if ([viewController isKindOfClass:([MainViewController class])])
{
interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
else
{
interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
}
}
}
return interfaceOrientation;
}
由于您无法再从子 ViewControllers 控制呈现的视图控制器的旋转设置,因此您必须以某种方式拦截当前在导航堆栈中的视图控制器。所以这就是我所做的:)。希望有帮助!