菜鸟教程小白 发表于 2022-12-12 09:32:10

ios - 当界面方向颠倒时呈现 View Controller


                                            <p><p>我的 iPhone 应用支持倒置界面方向:
当用户将手机倒置时(使主页按钮在顶部),整个用户界面会垂直翻转。</p>

<p>但是,当尝试从我的 ViewController<strong>A</strong> 呈现一些 ViewController<strong>B</strong> 时会出现问题(在此示例中,我呈现标准的邮件编写器 ViewController ):</p>

<pre><code>- (IBAction)buttonTapped:(id)sender {

    MFMailComposeViewController *mailVC = ;
    mailVC.mailComposeDelegate = self;

    ;

}
</code></pre>

<p>当这个方法被触发时,首先发生的事情是(蓝色) ViewController<strong>A</strong> 立即垂直翻转而没有动画,无论出于何种原因(如第一个屏幕截图所示)→ 为什么?</p >

<p>接下来呈现的 ViewController <strong>B</strong>从底部移入 View (应该如此)。</p>

<p> <img src="/image/5RExb.png" alt="presenting view controller B"/> </p>

<p>当关闭 ViewController<strong>B</strong> 时,它会立即翻转并移出 View 到顶部(而不是它来自的方向,如第二个屏幕截图所示)。</p>

<pre><code>- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    ;
}
</code></pre>

<p> <img src="/image/Y1ENn.png" alt="dismissing view controller B"/> </p>

<p><strong>这是 iOS 中的错误吗?或者我该如何避免这种奇怪的行为?</strong></p>

<p>(我只想要与普通纵向模式相同的标准动画来呈现 ViewController- 没有这些翻转。)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我猜 <code>MFMailComposeViewController</code> 不支持颠倒的界面方向,这就是它可能被解释为错误的原因。 <strong>但是</strong> Apple 建议不要对 iPhone 应用使用颠倒的界面方向。主要原因可以用这样的例子来解释:</p>

<ol>
<li>用户打开应用程序并将 iPhone 旋转到倒置方向。</li>
<li>此时有人调用他。</li>
<li>用户尝试接听电话却忘记将手机旋转到默认纵向。</li>
<li>因此 iPhone 的麦克风靠近用户的耳朵,扬声器靠近嘴巴。</li>
</ol>

<p>这不是用户友好的体验。<br/>
我会考虑在您的网站上使用或不使用颠倒方向。</p>

<p>但是,如果需要使用颠倒的界面方向,那么你会做这样的把戏:<br/>
为 <code>MFMailComposeViewController</code> 创建类别并覆盖方法 <code>supportedInterfaceOrientations</code> 以支持必要的方向并将其导入到创建 <code>MFMailComposeViewController</code> 的类中:</p>

<pre><code>// MFMailComposeViewController+Orientation.h file
#import &lt;MessageUI/MessageUI.h&gt;

@interface MFMailComposeViewController (Orientation)

@end

// MFMailComposeViewController+Orientation.m file
#import &#34;MFMailComposeViewController+Orientation.h&#34;

@implementation MFMailComposeViewController (Orientation)

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

@end
</code></pre>

<p><strong>注意:</strong>我不确定这个技巧是否会通过抛出 Apple 应用程序审批中心导致覆盖类别中现有方法的原因。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当界面方向颠倒时呈现 ViewController ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23249252/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23249252/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当界面方向颠倒时呈现 View Controller