ios - 限制某些 View 的自动旋转

标签 ios objective-c view autorotate

我的应用程序包含两个 TableView Controller 。在第一个中,我希望 View 能够左右旋转(除了纵向模式之外),但是在第二个 TableView Controller 中(我在点击第一个表中的单元格后导航到该 Controller )我想要它只能以纵向模式查看。我尝试了这段代码,但它不起作用,它一直在旋转。

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL) shouldAutorotate {
    return NO;
}

注意:我实际上从项目目标的“摘要”选项卡中启用了左/右/纵向方向。有修复吗?

最佳答案

为 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];
    }

然后在您的 Controller 中实现这些方法

第一:

- (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;
}

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

Settings

关于ios - 限制某些 View 的自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15835084/

相关文章:

ios - 用户不会显示在 Firebase 的“身份验证”选项卡上

ios - UIPageViewController 具有以编程方式生成的 View 数组

ios - PHImageManager 由于从照片库编辑而错误地获取照片

ios - 在 iOS 中,使用 UIViewController 构建通用 UI 组件是一个好习惯吗?

Android 从 CustomView 中绘制 TextView

ios - 播放音频文件时找不到 'PathForResource' 的过载

ios - 如何在操作表而不是全屏模态视图中显示操作扩展?

ios - WKwebview获取cookie的方法(IOS)

iphone - 如何使用 Storyboard在 iOS 中的 UIViewController 之间切换

twitter-bootstrap - 使用 Bootstrap 在模态视图中看不到某些 DIV