2

我正在从我的控制器展示 UIActivityViewController。我遇到了旋转问题。在选择了一些数据共享选项后,比如说“邮件”,邮件撰写视图控制器就会出现。但是如果我之后旋转设备并关闭邮件控制器,我的 VC 不会旋转到当前方向。我尝试为 UIActivityViewController 设置完成处理程序,如下所示:

1)

[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed){
            [UIViewController attemptRotationToDeviceOrientation];
        }];

或者

2)

[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed){
        UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        [self layoutForInterfaceOrientation:currentOrientation];
    }];

第一个解决方案根本没有帮助。第二种解决方案使导航栏保持不变(高度差 12 点)。

请告知如何处理这种情况。

4

3 回答 3

0

我已经解决了这个问题。首先,我应该说我的 VC 在 MailComposeVC 被关闭后总是旋转为纵向,而不管当前设备方向及其在显示邮件之前的状态如何......并且在 MailComposeVC 开启时自动旋转期间调用的唯一 VC 方法屏幕是viewWillLayoutSubviews/viewDidLayoutSubviews。(willAnimateRotationToInterfaceOrientation未调用!)因此,如果我的 VC 不可见,我重置导航栏以恢复其高度并调用我的自定义布局方法,如下所示:

(void) viewDidLayoutSubviews {
    // HACK: forcing the bars to refresh themselves - this helps to get the shorter version when switching to landscape and the taller one when going back!!!
    if (amIhidden) { 
        [self.navigationController setNavigationBarHidden:YES animated:NO];
        [self.navigationController setNavigationBarHidden:NO animated:NO];
        UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        [self layoutForInterfaceOrientation:currentOrientation];
    }
}

amIhidden BOOL在呈现MailComposeViewController时设置,如下:

UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
                                                        initWithActivityItems:@[self]
                                                        applicationActivities:nil];
amIhidden = YES;
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed){
        amIhidden = NO;
}];
[self presentViewController:activityViewController animated:YES completion:nil];

希望我的解决方案对其他人有所帮助!

于 2013-03-15T06:04:45.963 回答
0

如果接受的解决方案不适合您(像我一样),我已经回答了类似的问题,这个问题既简单又干净。Objective c - UIActivityViewController 方向模式

于 2014-07-29T21:49:14.620 回答
0

我在 Swift 3 中找到了一个解决方案。但我认为它也应该在 Objective-C 中工作。

AppDelegatefunctionapplication(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask中,返回特定视图控制器的正确方向。

棘手的部分是您必须挖掘出您希望方向基于的视图控制器。就像我的情况一样,什么时候UIActivityViewController出现,window?.currentViewController()并且window?.topMostController()都是UIActivityViewController. 因为我UIActivityViewController在其他地方使用,所以不能作为线索。然后我查了一下window?.currentViewController()?.presentingViewController,发现是一个UINavigationController。因为我知道我想要基于的视图控制器是那个的顶视图控制器,所以UINavigationController我的解决方案如下:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if window?.currentViewController() is MyLandscapeViewController{
        return [UIInterfaceOrientationMask.landscapeLeft, UIInterfaceOrientationMask.landscapeRight]
    }

    if let presenting = window?.currentViewController()?.presentingViewController{
        if let nav = presenting as? UINavigationController{
            if nav.topViewController is MyLandscapeViewController{
                return [UIInterfaceOrientationMask.landscapeLeft, UIInterfaceOrientationMask.landscapeRight]
            }
        }
    }

    return UIInterfaceOrientationMask.portrait;
}

使用此代码,在显示 MyLandscapeViewController 时选择共享选项后,屏幕不再旋转为纵向而是保持横向。

于 2017-04-06T07:45:38.910 回答