【问题标题】:UIModalPresentationCurrentContext with Transition?UIModalPresentationCurrentContext 与转换?
【发布时间】:2012-09-26 00:40:17
【问题描述】:

我正在尝试模态显示如下视图控制器:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"addPopover"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:vc animated:YES];

现在使用UIModalPresentationCurrentContext 意味着我可以用透明背景呈现这个视图,并在新视图后面看到我的另一个视图。但是,它使我无法通过过渡来呈现它。

任何想法为什么?或者我该如何解决这个问题?谢谢。

【问题讨论】:

  • 你正在展示的 ViewController 的展示风格是什么?
  • @bbodayle,我遇到了同样的问题,我已经尝试了两个 ViewController 上的所有演示样式/组合。

标签: iphone objective-c ipad uimodaltransitionstyle uimodalpresentationstyle


【解决方案1】:

全屏模式不应该让您看到底层。我知道很烦人。

根据您的代码,我假设“addPopover”实际上是一个全屏视图,其中您的内容作为一个小节,而其余部分是透明的。

试试这个:

vc.modalPresentationStyle = UIModalPresentationCurrentContext;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:vc animated:YES completion:NULL];

注意:我建议添加低 alpha 背景颜色,让用户知道当您将其弹出到顶部时,他们无法与视图的其余部分进行交互...

【讨论】:

  • 这里有什么不同?在目标视图上设置 modalTransitionStyle 没有任何区别,在 presentModalViewController 上使用 presentViewController 也没有任何区别。
  • 将过渡样式分配给vc而不是self,并使用presentViewController方法。如果没有看到你的故事板,很难诊断这是否不起作用。使用上述方法,您覆盖的 VC 需要与“self”大小相同(或能够调整大小)
  • 是的,在我的项目中,两个 ViewController 的大小相同。我试过你的代码,没有任何变化。这里需要将属性设置modalPresentationStyle = UIModalPresentationCurrentContext应用于self,而不是vc。我想我将不得不开始我自己的问题,而不是在这个问题上提供赏金,因为这里没有解决方案,即使它被标记为一个。
  • 在此处链接,我会尝试检查一下。抱歉,这对您没有帮助。
  • 但“PresentingViewController”(呈现新视图控制器的视图控制器)不会旋转。有什么办法吗?
【解决方案2】:

我可以通过在我的 UIWindow 的 rootViewController 上设置 modalPresentationStyle = UIModalPresentationCurrentContext 来实现这一点,如果我没有在这个 rootViewController 之上呈现任何新的全屏 viewController。我做了这样的事情:

UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:YES completion:nil];

我已经尝试使用 UINavigationController 作为我的窗口的 rootViewController 和各种其他自定义视图控制器。似乎只要窗口的 rootViewController 知道发生了什么,任何子视图控制器都可以调用 presentViewController:animated:completion: 而不会使底层视图变黑。

但是,假设您在窗口的 rootViewController 之上展示了另一个 viewController。而这个新的 viewController 会显示modalPresentationStyle = UIModalPresentationFullScreen(即占据屏幕),然后您必须在最顶层的 viewController 上调用 modalPresentationStyle = UIModalPresentationCurrentContext

回顾一下:

  • 如果您有 UINavigationController -> UIViewController(s)(它们可以被推入和弹出),那么您在 UINavigationController 上设置 modalPresentationStyle = UIModalPresentationCurrentContext
  • 如果您有 UINavigationController -> UIViewController -> new-UIViewController(modalPresentationStyle 设置为 UIModalPresentationFullScreen),那么您在 new-UIViewController 上设置 modalPresentationStyle = UIModalPresentationCurrentContext

希望这对你们也有用!

【讨论】:

  • 这对我有用。关键是在呈现视图控制器上设置 modalPresentationStyle,而不是正在呈现的视图控制器。
  • 如果你在 UISplitViewController 上展示它会怎样?
  • 动画一结束,我就得到了一个停电背景。我是UINavigationController 堆栈(当然是顶部)上的第二个视图控制器,我尝试在 vc 上设置 modalPresentationStyle = UIModalPresentationCurrentContext 以显示,selfself.navigationController 甚至 appDelegate.window.rootViewController 均无济于事。显然我错过了一些东西,有什么想法吗?
  • 这种方法的另一个问题是孔旋转中断。
  • 这在 iOS 7 中不适合我。iOS 8 没问题。
【解决方案3】:

先生。 T 的建议几乎完美,你只是失去了呈现的视图控制器上的过渡动画

由于它还设置了 appDelegates rootviewController 的呈现样式,它还将删除从该点呈现的任何视图的过渡动画。

我的解决方法是,每当我需要透明背景的 viewcontroller 关闭时,将 AppDelegates rootViewController 的呈现样式恢复为默认值

我有一个关闭presentedViewController的按钮,我还使用以下方法将rootViewController的演示样式设置回默认值:

    UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;

我希望这可以帮助其他人解决这个问题。

【讨论】:

    猜你喜欢
    • 2013-10-27
    • 1970-01-01
    • 2013-03-30
    • 2020-12-22
    • 2015-08-04
    • 1970-01-01
    • 2011-07-29
    • 2010-10-16
    • 2016-04-27
    相关资源
    最近更新 更多