如果您计划为所有视图控制器启用或禁用旋转,则无需子类化
UINavigationController
。 相反,请使用:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
在你的AppDelegate
中。
如果你计划在你的应用程序中支持所有方向,但是在父级视图控制器(例如UINavigationController
堆栈)中使用不同的方向,那么你应该使用
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
通过在您的父视图控制器中与AppDelegate
结合使用以下方法。
- (BOOL)shouldAutorotate
并且
- (NSUInteger)supportedInterfaceOrientations
但是,如果您计划在同一导航堆栈中的不同子视图控制器中具有不同的方向设置(像我一样),则需要检查导航堆栈中的当前视图控制器。
我在我的 UINavigationController
子类中创建了以下内容:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
int interfaceOrientation = 0;
if (self.viewControllers.count > 0)
{
DLog(@"%@", self.viewControllers);
for (id 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;
}
由于您不能再从子视图控制器中控制所呈现的视图控制器的旋转设置,因此必须以某种方式拦截当前导航堆栈中的视图控制器。这就是我所做的 :)。希望这有所帮助!