在视图中查看控制器控制器和方向iOS

我有iPAD应用程序,它具有UIViewController A作为导航控制器的根视图控制器。 现在我有三个视图控制器B,C,D作为ViewController A的子视图。

我希望B不要回应定位,而C和D应该回应定位。

目前在代码中,所有这些代码都会响应方向更改。

还有另一个答案,说两个独立的根ViewControllers,并将其添加到Windows视图。 其中一个不旋转和其他旋转。 我不能这样做,因为我有ViewController A中的标题切换B,C,D,使他们前面的viewController。

无论如何,请build议。

谢谢

你需要像这样inheritanceUINavigationController。

。H

#import <UIKit/UIKit.h> @interface UINavigationController (Rotation) - (BOOL)shouldAutorotate; - (NSUInteger)supportedInterfaceOrientations; @end 

.M

 #import "UINavigationController+Rotation.h" @implementation UINavigationController (Rotation) - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { if ([self visibleViewController] && [[self visibleViewController] isKindOfClass:[B class]]) { return UIInterfaceOrientationMaskAll; } return UIInterfaceOrientationMaskPortrait; } @end 

您可以从UINavigationController创build子类或为其创build类别。 并执行这个方法:

 -(BOOL)shouldAutorotate { return [self.topViewController shouldAutorotate]; } -(NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.topViewController preferredInterfaceOrientationForPresentation]; } 

然后,在你的控制器中,你应该用你想要的方向来实现这个方法。