菜鸟教程小白 发表于 2022-12-13 10:15:48

iphone - 以不同于设备方向的方向显示模态视图


                                            <p><p>我有一个支持所有设备方向的 iPhone 应用,但我有一个特定的模态视图,我只想以横向显示。</p>

<p>如果设备是横向的,它可以正常工作,但如果设备是纵向的,它不会以我想要的方式显示。</p>

<p>由于设备是纵向放置的,因此图像会被裁剪,并且左右两侧会超出显示屏的边缘。由于图像仅与窄尺寸一样高,因此顶部和底部显示了父 View 的背景。如果我然后将设备旋转到横向,那么一切都很好。</p>

<p>在我的研究中,我经常遇到 <strong>shouldAutorotateToInterfaceOrientation</strong>,但我相信这是一个仅在设备物理旋转时才会触发的事件,这不适用于我的情况。设备在物理上是纵向放置的,但我希望将 View 显示为横向放置。</p>

<p><strong>[ setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]</strong> 也提到了,但我在尝试使用它时从未见过有任何效果。</p>

<p>所以我想我的问题是,是否有一种简单的方法可以强制以不同于设备物理方向的方向初始显示 View ,以及具体如何操作。</p>

<p>我正在使用以下逻辑来模态显示我的 View :</p>

<pre><code>    TitleViewController *titleController = [ initWithNibName:@&#34;TitleViewController&#34; bundle:];
    titleController.delegate = self;
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    ;
    ;
</code></pre>

<p>我希望始终以横向显示模态视图,即使设备是纵向的。</p>

<p><strong>更新</strong></p>

<p>找到一个可行的解决方案:</p>

<pre><code>    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
      self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
      self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }
</code></pre>

<p>但是,在处理<strong>UIInterfaceOrientationPortraitUpsideDown</strong>方向时,它最初会以正确的方向显示 View ,然后将其转换回具有剪裁边的纵向并显示图像上方和下方的背景。 </p>

<p>我尝试将角度调整为 270、-90 和其他值,但在 <strong>UIInterfaceOrientationPortraitUpsideDown</strong> 方向时总是恢复为纵向。</p>

<p>为什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这种方法做了我需要的一切:</p>

<pre><code>    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
      self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
      self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }

    TitleViewController *titleController = [ initWithNibName:@&#34;TitleViewController&#34; bundle:];
    titleController.delegate = self;
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    ;
    ;
</code></pre>

<p>在呈现我的模态视图之前旋转方向。</p>

<p>我刚刚删除了对 <strong>UIInterfaceOrientationPortraitUpsideDown</strong> 的支持,因为不需要该方向。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 以不同于设备方向的方向显示模态视图,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8538430/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8538430/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 以不同于设备方向的方向显示模态视图