0

我有一个带有情节提要的 iOS 应用程序。我希望我的最后一个视图控制器始终保持纵向模式。我一直在阅读,我发现自从

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

已弃用我应该使用其他方法,例如

-(BOOL)shouldAutorotate  
-(NSInteger)supportedInterfaceOrientations
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

但我已经尝试了很多这种方法的组合,但我一直没能做到。那么请有人告诉我正确的方法吗?

4

3 回答 3

5

由于您的 UIViewController 嵌入在 UINavigationController 中,除非您自己转发呼叫,否则它永远不会被调用。(我认为 UINavigationController 有点缺陷)

子类 UINavigationController 像这样:

@interface RotationAwareNavigationController : UINavigationController

@end

@implementation RotationAwareNavigationController

-(NSUInteger)supportedInterfaceOrientations {
    UIViewController *top = self.topViewController;
    return top.supportedInterfaceOrientations;
}

-(BOOL)shouldAutorotate {
    UIViewController *top = self.topViewController;
    return [top shouldAutorotate];
}

@end
于 2013-05-23T18:23:04.537 回答
1

如果您在其他 UIViewControllers(即 UINavigationController 或 UITabBarController)中有 UIViewControllers,则需要将这些消息代理到您正在为其实现此行为的子对象。

您是否在实现中设置了断点以确保正在查询您的视图控制器?

于 2013-05-23T18:12:28.780 回答
1

在 AppDelegate 中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController) {
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }

    return orientations;
}

在您的视图控制器中:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
于 2014-05-15T10:03:07.840 回答