gpt4 book ai didi

objective-c - 在自定义容器 View 中旋转时,AutoLayout 不会调整 View 大小?

转载 作者:可可西里 更新时间:2023-11-01 05:46:05 25 4
gpt4 key购买 nike

我有一个非常基本的容器 View ,它包含一个侧边栏并交换了内容区域中的 View Controller (想想 UISplitView,但带有一个小图标侧边栏/垂直 UITabBar)。

容器 View Controller 使用 autoLayout 并在旋转时正确调整大小。Content viewController 1 使用autolayout 并且是用IB 制作的,所以它有一个xib 文件。Content viewController 2 继承自 UITableViewController,不使用 xib。

如果我将 viewController 1 指定为 Root View Controller 并旋转,则调整大小有效,这是我在 viewController 1 中获得的回调:

  • willRotateToInterfaceOrientation
  • 更新 View 约束
  • viewWillLayoutSubviews
  • didRotateFromInterfaceOrientation

但是,如果我将我的容器 View Controller 指定为 Root View Controller ,加载 viewController 1 并旋转,调整大小将不起作用。我只在 viewController 1 中得到以下回调:

  • willRotateToInterfaceOrientation
  • didRotateFromInterfaceOrientation

在我的 View Controller 容器中,这是我交换 View Controller 的方式:

[self addChildViewController:toViewController];
[toViewController didMoveToParentViewController:self];

// Remove the old view controller
[fromViewController willMoveToParentViewController:nil];
[fromViewController.view removeFromSuperview];
[fromViewController removeFromParentViewController];

// Add the new view
[self.contentContainerView addSubview:toViewController.view];

现在,我确实收到了轮换即将发生的回调,但似乎既没有调用 updateViewConstraints 也没有调用 viewWillLayoutSubviews。这解释了为什么没有发生调整大小,但是为什么一旦我将 View Controller 放入容器 View 中就没有调用这些方法?

我还尝试在我的容器中显式返回 YES

shouldAutomaticallyForwardAppearanceMethods

shouldAutomaticallyForwardAppearanceMethods

虽然这应该已经是默认值了。

此外,当在容器内旋转时,不是使用 IB 制作的 View Controller ( View Controller 2)会正确调整大小。但是,我没有在这个上明确使用 NSLayoutConstraints,所以我怀疑它在旋转时默认使用 Springs 和 Struts 来调整大小。

我是否需要在我的 View Controller 容器上转发一些其他事件以使自动布局 View Controller 在旋转时正确调整大小?

最佳答案

好的,我想我的 View Controller 容器中缺少这个方法:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.contentViewController.view.frame = self.contentContainerView.bounds;
}

虽然现在旋转时可以正确调整大小,但它仍然不会触发

updateViewConstraints

在我的 subview Controller 中。有趣

关于objective-c - 在自定义容器 View 中旋转时,AutoLayout 不会调整 View 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13177263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com