【问题标题】:UIViewController changes dimension after modal presentationUIViewController 在模态演示后更改尺寸
【发布时间】:2019-10-27 15:34:32
【问题描述】:

我有一个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 的大小?

【问题讨论】:

    标签: ios objective-c uiviewcontroller


    【解决方案1】:

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

    视图控制器的根视图总是调整大小以适应其分配的 空间。

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

    为此,您应该将 Storyboard 属性检查器中的视图控制器 Presentation 属性设置为 Over Full Screen。如果你想通过代码设置它,你设置视图控制器的.modalPresentationStyle = UIModalPresentationOverFullScreen。通过设置此项,底层视图控制器将在您呈现视图控制器后留在屏幕上,并在您的视图具有透明度的地方继续可见。

    Documentation for UIViewController

    Documentation for UIModalPresentationOverFullScreen

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2019-04-13
      • 2015-03-03
      • 2019-05-22
      • 1970-01-01
      相关资源
      最近更新 更多