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

标签 ios uiinterfaceorientation presentviewcontroller

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

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

- (IBAction)buttonTapped:(id)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)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self dismissModalViewControllerAnimated:YES];
}

dismissing view controller B

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

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

最佳答案

我猜 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/

相关文章:

ios - iPhone - 调整图像和两个标签以适应屏幕

ios - 控制 collectionView 从 collectionView Cell 中滚动

iphone - 为什么 [UIApplication sharedApplication].statusBarOrientation 返回错误值

iphone - 去掉界面翻转时出现的白色 "frame"?

ios7 presentviewcontroller 无法设置框架

ios - 为什么当单击数组中的按钮时应用程序崩溃?

ios - 如何将自定义字体实现到自定义键盘以在其他应用程序上使用?

iphone - iPad 应用强制旋转

ios - 在键盘处于事件状态时呈现模态视图 Controller

ios - 当 thirdViewController 在 Swift 中呈现时关闭 secondViewController