菜鸟教程小白 发表于 2022-12-12 13:39:22

IOS:在 UINavigationController 中锁定方向


                                            <p><p>在我的应用程序中,我使用纵向导航 Controller 启动 secondviewcontroller,然后在第四个viewcontroller 中完成,我应该在其中旋转 iPhone 以显示图像。
现在的问题是:
如果在目标中我以这种方式设置方向:
<img src="/image/X6saN.png" alt="enter image description here"/> </p>

<p>我可以用这个方法来旋转我的图片:</p>

<pre><code>- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
</code></pre>

<p>它工作正常,但我不能锁定属于导航 Controller 的所有 View ,因为它们都可以旋转纵向和横向。</p>

<p>第二个选项是以这种方式设置目标:
<img src="/image/Nmgqm.png" alt="enter image description here"/> </p>

<p>但是这个结构没有检测到方向:</p>

<p>会出现在 View 中:</p>

<pre><code>UIInterfaceOrientation orientation = .statusBarOrientation;
    ;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (BOOL)shouldAutorotate
{
      return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if ([ userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

      if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
      {

      }
      else
      {

      }
    }

    else{

      if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
      {

      }
      else
      {

      }

    }
}
</code></pre>

<p>什么是最好的解决方案?
谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我不禁觉得,如果我阅读了文档,它会为我节省一些时间,但对我来说,解决方案是 <strong>subclass</strong> UINavigaionController 并添加以下内容:</p>

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

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
</code></pre>

<p>由于这里实际上并没有发生任何实质性的事情,因此无需使用 coder 或其他任何东西来覆盖 init。您可以在之前引用导航 Controller 的任何地方引用此子类,包括 Interface Builder。</p></p>
                                   
                                                <p style="font-size: 20px;">关于IOS:在 UINavigationController 中锁定方向,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18334765/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18334765/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: IOS:在 UINavigationController 中锁定方向