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

标题: ios - 当界面方向颠倒时呈现 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:32
标题: ios - 当界面方向颠倒时呈现 View Controller

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

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

- (IBAction)buttonTappedid)sender {

    MFMailComposeViewController *mailVC = [MFMailComposeViewController new];
    mailVC.mailComposeDelegate = self;

    [self presentViewController:mailVC animated:YES completion:nil];

}

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

接下来呈现的 View Controller B从底部移入 View (应该如此)。

presenting view controller B

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

- (void)mailComposeControllerMFMailComposeViewController *)controller didFinishWithResultMFMailComposeResult)result errorNSError *)error {
    [self dismissModalViewControllerAnimated:YES];
}

dismissing view controller B

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

(我只想要与普通纵向模式相同的标准动画来呈现 View Controller - 没有这些翻转。)



Best Answer-推荐答案


我猜 MFMailComposeViewController 不支持颠倒的界面方向,这就是它可能被解释为错误的原因。 但是 Apple 建议不要对 iPhone 应用使用颠倒的界面方向。主要原因可以用这样的例子来解释:

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

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

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

// MFMailComposeViewController+Orientation.h file
#import <MessageUI/MessageUI.h>

@interface MFMailComposeViewController (Orientation)

@end

// MFMailComposeViewController+Orientation.m file
#import "MFMailComposeViewController+Orientation.h"

@implementation MFMailComposeViewController (Orientation)

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

@end

注意:我不确定这个技巧是否会通过抛出 Apple 应用程序审批中心导致覆盖类别中现有方法的原因。

关于ios - 当界面方向颠倒时呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249252/






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