【问题标题】:Force portrait orientation while pushing from landscape View Controller从横向视图控制器推送时强制纵向
【发布时间】:2013-01-15 23:49:54
【问题描述】:

应用支持:iOS6+

我的应用可以纵向和横向运行。但是 1 个控制器应该只适用于纵向。

问题是,当我处于横向并推动视图控制器时,新的视图控制器也处于横向状态,直到我将其旋转为纵向。然后它会按原样卡在纵向上。

是否可以始终以纵向显示?即使它的父母正在横向推动它?

以下所有代码都无济于事

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

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

【问题讨论】:

    标签: iphone objective-c orientation landscape portrait


    【解决方案1】:

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

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

    【讨论】:

    • +1:我需要在视图控制器出现之前“强制”它的方向,这样就成功了。希望我知道强制定向的“官方”方法,但在我知道之前,我会使用这种方法。
    • 如果您要多次调用视图,我建议将这些行添加到 viewWillAppear。
    • 但是这个 API 自 iOS6 以来已被弃用... :/
    • 当我实施此解决方案时,它在 Ipad 上不起作用,出现黑屏。
    • 有没有人在 iOS 8 上找到解决方案?如上所述,此 hack 不再有效。 Apple 在 iOS 8 中真的没有办法解决这个问题吗?
    【解决方案2】:

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

    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,只需将 UINavigationController 替换为 UITabBarController。 经过长时间的搜索,这个解决方案对我很有效!我和你现在的情况一样!

    编辑

    所以,我看到了你的样本。你需要做一些改变。 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;
    }
    

    【讨论】:

    • 我已经完成了相同的实现,但它不起作用。我已经创建了一个示例,您可以检查一下吗? github.com/tariq235/ForcePortrait
    • 我查看了您的代码,应该进行一些更改!我编辑了我的答案...检查一下然后告诉结果...
    • 首先感谢您花时间在我的源代码上。因为我需要在 Force Portrait 模式而不是 Landscape 模式下的第二个控制器,所以我将 MaskLandscape 的代码更改为 MaskPortrait。而且我还按照您的说明更新了 github 代码,但仍然无法正常工作。我的问题是,如果第一个控制器处于横向模式,然后单击按钮进行导航,那么第二个控制器在横向中不应该是可见的。它应该强制纵向旋转。
    • 我运行了你的代码,但它真的不起作用......我不明白为什么不起作用。稍后我会再次检查。你修好了吗?
    • 事实上这是一个不应该做的变通方法!但它确实有效!哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2020-02-02
    • 2010-12-21
    • 1970-01-01
    • 2013-09-28
    • 2013-12-17
    相关资源
    最近更新 更多