【问题标题】:restricting autorotation on certain views限制某些视图的自动旋转
【发布时间】:2013-04-05 13:21:25
【问题描述】:

我的应用程序包含两个表视图控制器。在第一个中,我希望视图能够左右旋转(除了纵向模式),但是在第二个表视图控制器中(我在从第一个表中点击一个单元格后导航到)我想要它只能在纵向模式下查看。我试过这段代码,但它不起作用,它一直在旋转。

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL) shouldAutorotate {
    return NO;
}

注意:我确实从项目目标的“摘要”选项卡中启用了左/右/纵向方向。有什么办法吗?

【问题讨论】:

标签: ios objective-c view autorotate


【解决方案1】:

为 UINavigationController 创建一个类别,其中包括以下方法:

(适用于 iOS 6 和 iOS 5)

- (BOOL)shouldAutorotate
    {
        return self.topViewController.shouldAutorotate;
    }

    - (NSUInteger)supportedInterfaceOrientations
    {
        return self.topViewController.supportedInterfaceOrientations;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }

然后在你的控制器中实现这些方法

第一:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    if (RUNNING_IPAD) {
        return UIInterfaceOrientationMaskAll;
    }
    else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    };
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    if (RUNNING_IPAD) {
        return YES;
    }
    else {
        return toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown;
    }
}

第二个:

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
       return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return NO;
}

项目的旋转设置应如下所示:

【讨论】:

  • 谢谢!我确实找到了一个更简单的答案,但我选择了这个,因为它同时支持 ios 5 和 6,我的只支持 6。
【解决方案2】:

其实我找到了解决问题的方法:

-(BOOL)shouldAutorotate{
    return YES;
}

-(NSInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

即使您在项目 Traget 的“摘要”选项卡中切换了左/右方向,这也会将视图限制为纵向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多