OGeek|极客世界-中国程序员成长平台

标题: ios - 限制某些 View 的自动旋转 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 03:54
标题: ios - 限制某些 View 的自动旋转

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

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL) shouldAutorotate {
    return NO;
}

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



Best Answer-推荐答案


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

(适用于 iOS 6 和 iOS 5)

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

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

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

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

第一:

- (BOOL)shouldAutorotate {
    return YES;
}

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

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

第二个:

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
       return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)toInterfaceOrientation {
        return NO;
}

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

Settings

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4