I have a view controller presented using UIModalPresentationCustom
presentation style. I use a custom UIViewControllerTransitioningDelegate
to present the view controller as a sidebar (so it slides in from the edge of the screen and does not occupy the full screen).
我有一个使用UIModalPresentationCustom演示风格呈现的视图控制器。我使用自定义UIViewControllerTransitioningDelegate将视图控制器显示为侧边栏(因此它从屏幕边缘滑入并且不占据整个屏幕)。
However when I then present another view controller from this one using UIModalPresentationFullScreen
— and then dismiss the full screen view controller, my underlying custom presented controller is suddenly resized to occupy the full screen. Does anyone know why this is the case?
然而,当我然后使用UIModalPresentationFullScreen呈现另一个视图控制器 - 然后关闭全屏视图控制器时,我的基础自定义显示控制器突然调整大小以占据整个屏幕。有谁知道为什么会这样?
Edit: this is essentially my animateTransition
method for presenting the sidebar — I've stripped out most of the code to make it readable. Basically it gets the container from the transitionContext, adds and animates the destination view controller's view to the container.
编辑:这实际上是我用于呈现侧边栏的animateTransition方法 - 我已经删除了大部分代码以使其可读。基本上它从transitionContext获取容器,添加目标视图控制器的视图并将其设置为容器动画。
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIView *container = transitionContext.containerView;
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *fromView = fromVC.view;
UIView *toView = toVC.view;
if( toVC.isBeingPresented )
{
[container addSubview:toView];
//... Animate some new frame for toView
//Call [transitionContext completeTransition:YES] on animation completion
}
else
{
//... Animate fromView out
//On completion remove fromView from superview
//Call [transitionContext completeTransition:YES] on animation completion
}
}
Edit 2: Doing a little more research, I notice that the frame of my custom presented view controller's view is being set when the view controller above it in the modal stack is dismissed. The following stack trace leads to the frame being set as full screen:
编辑2:做一些研究,我注意到我的自定义呈现视图控制器的视图的框架正在设置当模态堆栈中它上方的视图控制器被解除时。以下堆栈跟踪导致框架设置为全屏:
0 -[MyCustomPresentedViewControllerView setFrame:]
1 -[UIView(MPAdditions) setFrameOrigin:]
2 -[UIViewControllerAccessibility(SafeCategory) dismissViewControllerWithTransition:completion:]
3 -[UIViewController dismissViewControllerAnimated:completion:]
3 个解决方案
#1
4
Changing the presentation style by assigning:
通过分配来更改演示文稿样式:
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
results in the modal presentation controller occupying a fraction of the screen, the same fraction as the original presented view controller of the UIPresentationController
.
However, using viewController.modalPresentationStyle = UIModalPresentationOverFullScreen
is better for this case, since his modal controller is full screen.
导致模态呈现控制器占据屏幕的一小部分,与UIPresentationController的原始呈现视图控制器相同。但是,使用viewController.modalPresentationStyle = UIModalPresentationOverFullScreen对于这种情况更好,因为他的模态控制器是全屏的。
#2
2
Try to use:
尝试使用:
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
It helped me in iOS 8.1
它在iOS 8.1中帮助了我
#3
2
I am experiencing the same issue. I've tried debugging it using symbolic breakpoints and there seem to be some internal call on some kind of layout manager that does this.
我遇到了同样的问题。我已经尝试使用符号断点调试它,并且似乎在某种布局管理器上有一些内部调用来执行此操作。
While I wasn't able to "solve" this (it seems to me like a bug in the SDK), I was able to come up with a workaround that fixes this. Basically, you have to set the correct dimensions of the presented view at two opportune times. Like this:
虽然我无法“解决”这个问题(在我看来像SDK中的一个错误),但我能够找到解决此问题的解决方法。基本上,您必须在两个适当的时间设置所呈现视图的正确尺寸。喜欢这个:
In the view controller that was presented using your custom UIPresentationController, add this:
在使用自定义UIPresentationController呈现的视图控制器中,添加以下内容:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
dispatch_async(dispatch_get_main_queue(), ^{
self.view.frame = [self.presentationController frameOfPresentedViewInContainerView];
});
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.view.frame = [self.presentationController frameOfPresentedViewInContainerView];
}
And if you are wondering: yes, you do need it to do in two places. Because weirdly enough, when I did it only in the viewDidAppear async block, it got broken (resized to fullscreen) once the animation finished.
如果你想知道:是的,你确实需要它在两个地方做。因为奇怪的是,当我只在viewDidAppear异步块中执行它时,一旦动画结束它就会被破坏(调整为全屏)。
#1
4
Changing the presentation style by assigning:
通过分配来更改演示文稿样式:
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
results in the modal presentation controller occupying a fraction of the screen, the same fraction as the original presented view controller of the UIPresentationController
.
However, using viewController.modalPresentationStyle = UIModalPresentationOverFullScreen
is better for this case, since his modal controller is full screen.
导致模态呈现控制器占据屏幕的一小部分,与UIPresentationController的原始呈现视图控制器相同。但是,使用viewController.modalPresentationStyle = UIModalPresentationOverFullScreen对于这种情况更好,因为他的模态控制器是全屏的。
#2
2
Try to use:
尝试使用:
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
It helped me in iOS 8.1
它在iOS 8.1中帮助了我
#3
2
I am experiencing the same issue. I've tried debugging it using symbolic breakpoints and there seem to be some internal call on some kind of layout manager that does this.
我遇到了同样的问题。我已经尝试使用符号断点调试它,并且似乎在某种布局管理器上有一些内部调用来执行此操作。
While I wasn't able to "solve" this (it seems to me like a bug in the SDK), I was able to come up with a workaround that fixes this. Basically, you have to set the correct dimensions of the presented view at two opportune times. Like this:
虽然我无法“解决”这个问题(在我看来像SDK中的一个错误),但我能够找到解决此问题的解决方法。基本上,您必须在两个适当的时间设置所呈现视图的正确尺寸。喜欢这个:
In the view controller that was presented using your custom UIPresentationController, add this:
在使用自定义UIPresentationController呈现的视图控制器中,添加以下内容:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
dispatch_async(dispatch_get_main_queue(), ^{
self.view.frame = [self.presentationController frameOfPresentedViewInContainerView];
});
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.view.frame = [self.presentationController frameOfPresentedViewInContainerView];
}
And if you are wondering: yes, you do need it to do in two places. Because weirdly enough, when I did it only in the viewDidAppear async block, it got broken (resized to fullscreen) once the animation finished.
如果你想知道:是的,你确实需要它在两个地方做。因为奇怪的是,当我只在viewDidAppear异步块中执行它时,一旦动画结束它就会被破坏(调整为全屏)。