弹回 parentVC 后 parentVC 和 childVC 中的帧不正确

Frame incorrect in parentVC and childVC after popping back to parentVC

我遇到的问题是我的视图框架在 return 到 viewController 之后通过弹出当前框架完全错误。

我的视图层级如下:

UITabbarController,
    UINavigationController
        HomeSwipeViewController (need as I cant put a UIPageViewController straight into a navController)
            UIPageViewController
                 HomeViewController

在 HomeSwipeViewController 中,我将页面ViewController 嵌入到 containerView 中,containerView 有约束并且应该是完整的(在情节提要中设置):

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

[self.containerView addSubview:self.pageController.view];
[self addChildViewController:self.pageController];
[self.pageController didMoveToParentViewController:self];

[self.pageController.view addFullScreenConstraint]; // full to superView

在家里面ViewController 是一个 collectionView,您可以点击它,然后我将 ViewController 推到 UINavigationController 上。这工作正常,但当我弹回 HomeSwipeViewController 时,所有帧都不正确。

在homeSwipe的viewDidAppear中打印出来的日志ViewController:

首次出现:

[12234:152630] containerView: <UIView: 0x7fea0046cdb0; frame = (0 0; 320 464); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fea0046c450>>
[12234:152630] pageController.view: <_UIPageViewControllerContentView: 0x7fea0290b690; frame = (0 0; 320 464); clipsToBounds = YES; opaque = NO; autoresize = W+H; layer = <CALayer: 0x7fea02909f60>>
[12234:152630] self.view: <UIView: 0x7fea0285d2d0; frame = (0 64; 320 504); autoresize = W+H; layer = <CALayer: 0x7fea0285d1d0>>

弹出后:

[12234:152630] containerView: <UIView: 0x7fea0046cdb0; frame = (0 0; 0 0); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fea0046c450>>
[12234:152630] pageController.view: <_UIPageViewControllerContentView: 0x7fea0290b690; frame = (0 0; 0 0); clipsToBounds = YES; opaque = NO; autoresize = W+H; layer = <CALayer: 0x7fea02909f60>>
[12234:152630] self.view: <UIView: 0x7fea0285d2d0; frame = (0 64; 320 455); autoresize = W+H; layer = <CALayer: 0x7fea0285d1d0>>

我试过在 viewDidAppear 中调用 setNeedsDisplay、setNeedsLayout 和 setNeedsUpdateConstraints。在 viewDidLayoutSubViews 中以编程方式添加约束。

控制台中未记录任何破坏的约束。

我认为这与嵌入 viewController 有关,因为当我删除 UIPageViewController 并将 homeViewContoller 添加为子 ViewController 时,会发生相同的结果。

如果我什么都不嵌入,就没有问题。

所有视图都有 translatesAutoresizingMaskIntoConstraints = NO

我通过在 viewDidLayoutSubviews 中设置父视图控制器 (HomeSwipeViewController) 的约束来解决这个问题。我只在 self.view.translatesAutoresizingMaskIntoConstraints = YES;

时使用它

我认为这是因为该项目同时使用了已现代化为自动布局的视图和基于框架(自动调整)的视图。它看起来像用没有自动布局的视图推送 VC,删除来自约束的 VC 或者它可能只是停用自动布局引擎。

- (void)viewDidLayoutSubviews {

    if (self.view.superview) {

        self.view.translatesAutoresizingMaskIntoConstraints = NO;

        [self.view.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[view]|" options:kNilOptions metrics:nil views:@{@"view": self.view}]];
        [self.view.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-64-[view]|" options:kNilOptions metrics:nil views:@{@"view":self.view}]];

    }

    [super viewDidLayoutSubviews];
}  

任何关于为什么这有效的见解都会很酷!