UIActivityViewControllerclosures后的正确方向
我从我的控制器呈现UIActivityViewController。 我遇到了旋转问题。 在select了一些数据共享选项之后,让我们说“邮件”,邮件撰写视图控制器被呈现。 但是,如果我旋转设备并closures邮件控制器之后,我的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]; }];
第一个解决scheme根本没有帮助。 第二个解决scheme留下导航栏不旋转(高度差12个点)。
请告知如何处理这种情况。
我已经解决了这个问题。 首先,我应该说我的VC总是旋转到肖像后,不pipe当前设备的方向和它的状态,在显示邮件之前解除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]; } }
呈现MailComposeViewController时设置amIhidden BOOL
,如下所示:
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];
希望,我的解决scheme将帮助别人!
如果接受的解决scheme不适合你(像我),我已经回答了类似的问题,这是容易和清洁。 Objective c – UIActivityViewController方向模式
我想出了Swift 3中的一个解决scheme。但我认为它也应该在Objective-C中工作。
在AppDelegate
函数application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
,为特定的视图控制器返回正确的方向。
棘手的部分是,你必须挖出你想要的方向基于视图控制器。 就像我的情况一样,当提供UIActivityViewController
时, window?.currentViewController()
和window?.topMostController()
都是UIActivityViewController
。 因为我在其他地方使用UIActivityViewController
,我不能用它作为线索。 然后我检查window?.currentViewController()?.presentingViewController
,发现它是一个UINavigationController
。 由于我知道我想基于的视图控制器是该UINavigationController
的顶部视图控制器,我的解决scheme如下:
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时select共享选项后保持横向。