菜鸟教程小白 发表于 2022-12-12 03:54:52

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


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

<pre><code>- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL) shouldAutorotate {
    return NO;
}
</code></pre>

<p>注意:我确实从项目目标的“摘要”选项卡中启用了左/右/纵向。有什么办法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>为 UINavigationController 创建一个类别,包括以下方法:</p>

<p>(适用于 iOS 6 和 iOS 5)</p>

<pre><code>- (BOOL)shouldAutorotate
    {
      return self.topViewController.shouldAutorotate;
    }

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

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
      return ;
    }
</code></pre>

<p>然后在你的 Controller 中实现这些方法</p>

<p>第一:</p>

<pre><code>- (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;
    }
}
</code></pre>

<p>第二个:</p>

<pre><code>- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
       return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
      return NO;
}
</code></pre>

<p>项目的旋转设置应如下所示:</p>

<p> <img src="/image/QnYNN.png" alt="Settings"/> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 限制某些 View 的自动旋转,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15835084/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15835084/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 限制某些 View 的自动旋转