【问题标题】:How to lock orientation in UINavigationControllers如何在 UINavigationControllers 中锁定方向
【发布时间】:2012-09-26 11:33:09
【问题描述】:

由于ShouldAutorotateToInterfaceOrientationiOS 6 中已弃用,我无法在我的应用程序中锁定方向。在我的应用程序中,我有多个视图的UINavigationControllers,一些视图需要同时支持纵向和横向,而其他视图只需要支持纵向。我该如何解决这个问题,请给我一些建议。

谢谢

【问题讨论】:

    标签: iphone uinavigationcontroller orientation ios6


    【解决方案1】:

    此功能仅适用于iOS 6

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    
    {
    
        return UIInterfaceOrientationMaskPortrait;
    
    }
    

    【讨论】:

    【解决方案2】:

    您可以通过继承 UINavigationController 来锁定方向。

    Here is an excellent link on how to do that.

    然后在子类 UIViewController 中重写以下方法来实现锁定(在本例中为纵向锁定)。

    -(BOOL)shouldAutorotate
    {
        return NO;
    }
    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait;
    }
    

    【讨论】:

    • 绝对没有任何效果。
    【解决方案3】:

    在 viewDidLoad 中添加以下代码

    UIViewController *viewController = [[UIViewController alloc] init];
    [self presentModalViewController:viewController animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    

    用于锁定纵向方向

    • 选择属性检查器选项卡。
    • 将方向设置为纵向。

    添加功能

     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return ( UIInterfaceOrientationIsPortrait(interfaceOrientation)); 
    }
    

    用于锁定横向

    • 选择属性检查器选项卡。
    • 将方向设置为横向。

    添加功能

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return ( UIInterfaceOrientationIsLandscape(interfaceOrientation) ); 
    }
    

    【讨论】:

      【解决方案4】:

      我找到了 ShouldAutorotateToInterfaceOrientation 的替代品。我建议您浏览这些链接 -

      http://www.roostersoftstudios.com/2012/09/21/ios6-autorotation-changes

      谢谢

      【讨论】:

        猜你喜欢
        • 2011-04-23
        • 1970-01-01
        • 2011-02-19
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多