【问题标题】:Child container controller not receiving viewWillLayoutSubviews after Rotation event旋转事件后子容器控制器未收到 viewWillLayoutSubviews
【发布时间】:2013-03-10 17:44:03
【问题描述】:

我有一个视图控制器,它是容器控制器的子级,我想知道当设备上的方向发生变化时如何调整视图大小。 子控制器没有收到预期的 viewWillLayoutSubviews 事件。

我的印象是,当视图的边界发生变化时会调用“viewWillLayoutSubviews”,但 ChildViewController 的 willRotateToInterfaceOrientation 和 didRotateFromInterfaceOrientation 事件之间的边界值保持不变。 为什么会这样?

我在轮换期间看到的视图特定事件是:

[ParentViewController viewDidLoad]
[ParentViewController viewWillLayoutSubviews]
[ParentViewController layoutHomeForStartup]
[ParentViewController viewDidAppear:]
[ParentViewController showLoginView]

[ChildViewController viewDidLoad]
[ChildViewController didMoveToParentViewController:]

[ParentViewController viewWillLayoutSubviews]

[ChildViewController viewWillLayoutSubviews]
[ChildViewController viewDidAppear:]
[ChildViewController showLoginPanel]

[ParentViewController willRotateToInterfaceOrientation:duration:]
[ChildViewController willRotateToInterfaceOrientation:duration:]

[ParentViewController viewWillLayoutSubviews]
// QUESTION: Why doesn't the childViewController not receive viewWillLayoutSubviews?

[ParentViewController didRotateFromInterfaceOrientation:]
[ChildViewController didRotateFromInterfaceOrientation:]

这是我将子控制器添加到父控制器的方式:

// ParentViewController:
- (void)viewDidLoad
{
    ChildViewController *childViewController = [ChildViewController alloc] initWithNibName...];

    [childController willMoveToParentViewController:self];
    [self addChildViewController:childController];
    [view addSubview:childController.view];
    [childController didMoveToParentViewController:self];
}

因为我仍然针对 iOS 5.0+ 设备,所以我也实现了:

- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers 
{
    return YES;
}

shouldAutomaticallyForwardAppearanceMethodsshouldAutomaticallyForwardRotationMethods 也已实现并返回 YES(对于那些使用 iOS 6+ 的用户);

我不确定在容器控制器的子视图控制器中处理方向变化的最佳方法是什么。

【问题讨论】:

    标签: iphone objective-c ios5 uiviewcontroller


    【解决方案1】:

    1.

    automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers其实是两种方法:

    它们被称为shouldAutomaticallyForwardRotationMethodsshouldAutomaticallyForwardAppearanceMethods

    即使是 IOS5 下的 1 种方法,您也必须为 IOS6 实现较新的变体

    2.

    孩子们的界限似乎并没有改变你在 cmets 中所说的,这是有道理的。所以在视图控制器的视图上调用setNeedsLayout 让它们自己布局:)

    【讨论】:

    • 它们在 iOS6 中变成了 2 种方法,由于我仍在启动运行 iOS 5 的设备,因此我无法使用它们。
    • 对不起。但是你也需要实现这些。与houldRotate 的东西一样。生病编辑我的答案更明确。
    • 我认为问题在于子视图的边界没有改变。我的理解是,当边界发生变化时,会调用 viewWillLayoutSubviews。
    • 好吧,这也可能是真的......它们没有旋转......在视图上调用 setNeedsLayout 以强制它布局其子视图
    • 所以在 willRotateToInterfaceOrientation 中调用 setNeedsLayout 或者从父 viewController 的 viewWillLayoutSubviews 中调用子的 setNeedsLayout?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2020-06-03
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多