ios - UIViewController 在模式呈现后更改尺寸

标签 ios objective-c uiviewcontroller

我有一个 UIViewController,它使用自定义转换呈现,并且根据设计,它仅填充屏幕高度的 90%。

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

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

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

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

最佳答案

我认为问题在于您修改了 View Controller Root View 的大小,尽管它是由 View Controller 处理的。 UIViewController 的文档说:

A view controller’s root view is always sized to fit its assigned space.

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

为此,您应该将 Storyboard属性检查器中的 View Controller 演示属性设置为全屏。如果您想通过代码设置它,请设置 View Controller 的 .modalPresentationStyle = UIModalPresentationOverFullScreen。通过设置此选项,在您呈现 View Controller 后,底层 View Controller 将保留在屏幕上,并在 View 具有透明度的情况下继续可见。

Documentation for UIViewController

Documentation for UIModalPresentationOverFullScreen

关于ios - UIViewController 在模式呈现后更改尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573738/

相关文章:

iOS - 应用程序初始 ViewController 实例

ios - 尽管 cellForRowAtIndexPath 返回有效单元格,但 UITableView 崩溃

ios - 在 Atlas.h 中找不到 ATLAddressBarViewController.h

ios - 在我的 Controller 中未调用 supportedInterfaceOrientations

ios - 我正在解析 JSON 响应

objective-c - union 结构 : printing struct member of type uint32_t skips two bytes and prints wrong value

ios - 在警报 View 中,如何更改背景颜色和显示 View 底部?

iphone - 在 PhoneGap IOS 上缓存图像的最佳方式

ios - 整个应用程序都使用相同的 UISearchBar?

ios - 当不再显示 UIViewController 时如何继续 CAKeyframeAnimation?