首页 文章

从横向视图控制器推动时强制纵向方向

提问于
浏览
30

应用支持:iOS6

我的应用程序适用于纵向和横向 . 但是1个控制器应该仅适用于肖像 .

问题是,当我在横向并推动视图控制器时,新的视图控制器也处于横向状态,直到我将其旋转为纵向 . 然后,它应该是画像 .

是否有可能始终以纵向显示?即使它的父母在风景中推动它?

以下所有代码都无济于事

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

这段代码一直有效,除非我不是从风景推动How to force a UIViewController to Portrait orientation in iOS 6

2 回答

  • 31

    我通过在ViewDidLoad中添加以下行来解决这个问题

    UIViewController *c = [[UIViewController alloc]init];
    [self presentViewController:c animated:NO completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
    
  • 3

    首先,您需要创建一个类别:

    UINavigationController Rotation_IOS6.h

    #import <UIKit/UIKit.h>
    
    @interface UINavigationController (Rotation_IOS6)
    
    @end
    

    UINavigationController Rotation_IOS6.m:

    #import "UINavigationController+Rotation_IOS6.h"
    
    @implementation UINavigationController (Rotation_IOS6)
    
    -(BOOL)shouldAutorotate
    {
        return [[self.viewControllers lastObject] shouldAutorotate];
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return [[self.viewControllers lastObject] supportedInterfaceOrientations];
    }
    
    @end
    

    然后,在类中实现这些方法,使其只是横向:

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    

    如果您正在使用UITabBarController,只需替换UITabBarController的UINavigationController即可 . 经过长时间的搜索,这个解决方案对我很有用!我和你现在的情况一样!

    EDIT

    所以,我看到了你的样本 . 你需要做一些改变 . 1 - 为UINavigationController类别创建一个新类 . 将类命名为UINavigationController Rotation_IOS6(.h和.m)2 - 您不需要实现方法 preferredInterfaceOrientationForPresentation . 您的类别应如下所示:

    #import "UINavigationController+Rotation_IOS6.h"
    
    @implementation UINavigationController (Rotation_IOS6)
    
    -(BOOL)shouldAutorotate
    {
        return [[self.viewControllers lastObject] shouldAutorotate];
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return [[self.viewControllers lastObject] supportedInterfaceOrientations];
    }
    
    @end
    

    3 - 在您希望仅在横向中旋转的类中,将其包含在实现中,如下所示:

    // Rotation methods for iOS 6
    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    

    4 - 我建议在你想要的类中包含iOS 5中自动旋转的方法:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
    }
    

相关问题