简体   繁体   English

UIViewController在模态表示后更改维度

[英]UIViewController changes dimension after modal presentation

I have a UIViewController which is presented using a custom transition, and by design it only fills 90% of the screen's height. 我有一个UIViewController ,它使用自定义过渡呈现,并且通过设计它只填充屏幕高度的90%。

This appears fine, and I've never had any issues with it. 这似乎很好,我从来没有遇到任何问题。 Let's call it View A. Now I am trying to present a full screen modal view on top of this, let's call that View B. This appearance works, but when View B is dismissed, View A reappears, but has been expanded to fill the entire bounds of the screen. 让我们称它为View A.现在我试图在此基础上呈现全屏模态视图,让我们称之为View B.这种外观有效,但当View B被解除时,View A重新出现,但已经扩展为填充屏幕的整个边界。

Here's the presentation code I'm using: 这是我正在使用的演示代码:

- (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];
    }];
    ...
}

Has anyone seen this before, and know how to stop the system from resizing View A? 有没有人见过这个,知道如何阻止系统调整View A的大小?

I think the problem is that you modify the size of the view controller's root view, although it is handled by the view controller. 我认为问题在于您修改了视图控制器的根视图的大小,尽管它由视图控制器处理。 The documentation for UIViewController says: UIViewController的文档说:

A view controller's root view is always sized to fit its assigned space. 视图控制器的根视图的大小始终适合其指定的空间。

Why not add another (fully transparent) view as a child to the root view, where you place all your content? 为什么不将另一个(完全透明的)视图作为子视图添加到根视图中,您放置所有内容? Doing so let's you keep the root view at 100% while changing the new view's size to 90% when you want to. 这样做,让您将根视图保持在100%,同时将新视图的大小更改为90%。 If I understand you correctly this will accomplish the same thing without touching the root view. 如果我理解正确的话,这将完成相同的事情而不触及根视图。

For this to work you should set the view controllers Presentation property in the Storyboard attribute inspector to Over Full Screen . 为此,您应将Storyboard属性检查器中的视图控制器Presentation属性设置为Over Full Screen If you want to set it by code you set the view controller's .modalPresentationStyle = UIModalPresentationOverFullScreen . 如果要通过代码进行设置,请设置视图控制器的.modalPresentationStyle = UIModalPresentationOverFullScreen By setting this the underlying view controller will stay on screen after you have presented your view controller and continue to be visible where you have transparency in your view. 通过设置此选项,底层视图控制器将在您呈现视图控制器后保留在屏幕上,并在视图中具有透明度的位置继续显示。

Documentation for UIViewController UIViewController的文档

Documentation for UIModalPresentationOverFullScreen UIModalPresentationOverFullScreen的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM