对于某些情况,我建议阅读以下内容:
非常相关的问题:"From View Controller" disappears using UIViewControllerContextTransitioning
非常相关的答案:https://stackoverflow.com/a/25901154/751268
我正在尝试实现一个自定义 View Controller 转换,该动画使新 View Controller 覆盖一半屏幕,同时将呈现 View Controller 缩小到 90%(在窗口中居中,在呈现 View Controller 下方)。
首先,我的问题是 viewFromKey:
返回 nil
.为了解决这个问题,答案提到:
If you want to animate the presenting view controllers's view you should consider using
UIModalPresentationFullscreen
style or continue usingUIModalPresentationCustom
and implement your own subclass ofUIPresentationController
withshouldRemovePresentersView
returning YES.
我这样做了,
viewFromKey:
不返回 nil
不再,但是现在呈现的 View Controller 完全消失了(考虑到我明确地说应该通过实现 shouldRemovePresentersView
,这是有道理的)。我将呈现 View Controller 的 View 添加到容器 View 中,但它仍然被删除。我还应该做些什么来使它正常工作吗?
这是一些相关的代码:
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
BOOL show = self.isPresentation;
UIView *menuView = show ? toView : fromView;
UIView *backView = show ? fromView : toView;
UIView *containerView = [transitionContext containerView];
[containerView addSubview:backView];
[containerView addSubview:dimmedView];
[containerView addSubview:menuView];
// Adjust transforms, alpha and perform animations
我以为通过返回
YES
来自 shouldRemovePresentersView
并手动将其添加到 containerView
,应该可以解决问题,但是 backView
无论如何都会被删除...
最佳答案
我正在添加另一个答案,因为我的回复太长,无法放入评论中。
首先是viewForKey
在 iOS8 中可用,所以除非你只针对 iOS8(为什么?)你不应该使用它,或者在检查 UIViewControllerContextTransitioning
之后使用它。响应该选择器并使用 viewControllerForKey
适用于 iOS7。
话虽如此,在我看来这是一个错误,我解释了我自己:
如果您查看 UIPresentationController
头文件,你会看到它说
// Indicate whether the view controller's view we are transitioning from will be removed from the window in the end of the
// presentation transition
// (Default: YES)
- (BOOL)shouldRemovePresentersView;
如您所见,默认值为"is",因此仅当您特别想说“否”时才应覆盖它。
但是,您是对的,如果没有将其明确设置为 YES,
viewForKey
对于UITransitionContextFromViewControllerKey
仍然为零。我认为您应该为此填写错误报告,现在使用
viewControllerForKey
可以使用(这没什么问题),因为它没有被弃用并且在两个操作系统版本中都可以正常工作。这很可能是一个错误的原因是
viewForKey
应该返回 UITransitionContextFromViewControllerKey
的 View 当 shouldRemovePresentersView
显式设置为 否 而不是 是 .我的 2 美分
关于ios - 在 UIPresentationController 中动画呈现 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26851471/