UIView控制器在模态演示后更改维度



我有一个使用自定义过渡呈现的UIViewController,根据设计,它只填充屏幕高度的 90%。

这看起来很好,我从来没有遇到过任何问题。我们称之为视图 A。现在我试图在此基础上呈现一个全屏模态视图,我们称之为视图 B。此外观有效,但当视图 B 关闭时,视图 A 会重新出现,但已展开以填充屏幕的整个边界。

这是我正在使用的演示代码:

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
...
// Presentation
const CGFloat viewHeight = (screenBounds.size.height * 0.9);
const CGRect beginFrame = CGRectMake(0, screenBounds.size.height, screenBounds.size.width, viewHeight);
const CGRect finalFrame = CGRectMake(0, (screenBounds.size.height - viewHeight), screenBounds.size.width, viewHeight);
// Dim
self.dimmedView.alpha = 0.0;
[transitionContext.containerView addSubview:self.dimmedView];
[transitionContext.containerView addConstraints:[NSLayoutConstraint allConstraintsFromViewToSuperview:self.dimmedView inset:UIOffsetZero]];
// Prepare
UIView * const toView = toVC.view;
toView.frame = beginFrame;
[transitionContext.containerView addSubview:toView];
// Animate
[UIView animateWithDuration:kAnimationDuration delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:0.25 options:0 animations:^{
toView.frame = finalFrame;
self.dimmedView.alpha = 0.6;
self.tabBarController.view.layer.cornerRadius = 8.0;
self.tabBarController.view.transform = CGAffineTransformMakeScale(0.95, 0.95);
} completion:^(BOOL finished) {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
[offshootView removeFromSuperview];
}];
...
}

以前有没有人见过这个,并且知道如何阻止系统调整视图 A 的大小?

我认为问题是您修改了视图控制器根视图的大小,尽管它由视图控制器处理。UIViewController的文档说:

视图

控制器的根视图始终调整大小以适合其分配的大小 空间。

为什么不将另一个(完全透明)视图作为子视图添加到根视图中,您可以在其中放置所有内容?这样做可以让您将根视图保持在 100%,同时在需要时将新视图的大小更改为 90%。如果我理解正确,这将在不接触根视图的情况下完成同样的事情。

为此,您应该将情节提要属性检查器中的视图控制器"演示文稿"属性设置为Over Full Screen。如果要通过代码进行设置,请设置视图控制器的.modalPresentationStyle = UIModalPresentationOverFullScreen。通过设置此设置,底层视图控制器将在您呈现视图控制器后保留在屏幕上,并在视图中具有透明度的地方继续可见。

UIViewController 的文档

UIModalPresentationOverFullScreen 的文档

最新更新