ios - 在 UIViewController 上设置方向

标签 ios uiviewcontroller orientation

我正在尝试为 UIViewController 设置方向并禁止该 UIViewController 上的所有方向更改。重要的是要注意该应用程序允许所有方向,但在这个单一 View Controller 上我只想允许纵向。

我正在使用 iOS 8,该应用程序适用于 iOS 6 及更高版本。所描述的行为发生在 iOS 7 和 8 中。我无法在 iOS 6 中测试它,所以我不确定它是否发生在 iOS 6 中。

我实现了以下方法:

- (BOOL)shouldAutorotate
{
    return NO; // I've also tried returning YES - no success
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait; // I've also tried UIInterfaceOrientationPortrait - no success
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait; // Never called
}

我已经能够为模态呈现的 View Controller 设置方向,但我没有为该项目以模态呈现 View Controller 。这甚至可以做到吗?

如何在不影响其他 UIViewController 的方向的情况下指定一个 UIViewController 的方向?这甚至可以做到吗?

最佳答案

假设您的 View Controller 嵌入在导航 Controller 中,您需要使用 UINavigationController 的自定义子类并添加:

- (BOOL)shouldAutorotate;
{
    BOOL shouldAutorotate;
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)])
    {
        shouldAutorotate = [self.topViewController shouldAutorotate];
     }
     else {
        shouldAutorotate = [super shouldAutorotate];
      }
      return shouldAutorotate;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger supportedOrientations;
    if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        supportedOrientations = [[self topViewController] supportedInterfaceOrientations];
    }
    else {
        supportedOrientations = [super supportedInterfaceOrientations];
    }
    return supportedOrientations;
}

在你的导航 Controller 中。你的 View Controller 中的方法很好,所以让它们保持原样。这适用于 iOS 7-8(尚未在 iOS6 中测试)

关于ios - 在 UIViewController 上设置方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26386574/

相关文章:

ios - Objective-C prepareForSegue 问题

ios - Xcode 关闭后找不到框架

iphone - 是否可以在用户没有在iOS中点击本地通知的情况下自动启动应用程序?

ios - 无法在Swift中设置IndexPath行

java - Android getOrientation() 方法返回错误结果

ios - 默认纵向,一些特殊的屏幕如何自动旋转或强制某些屏幕横向?

ios - 将数据从自定义 UITableViewCell 发送到详细信息 ViewController - Swift

来自主 UIViewController 的 IOS 模态视图

ios - 使用 NSNotificationCenter 在 VC 之间发送数据

android - Android手机水平时确定滚动角度(上/下)?