我有一个视图控制器,它是容器控制器的子级,我想知道当设备上的方向发生变化时如何调整视图大小。子控制器未收到预期的 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;
}
shouldAutomaticallyForwardAppearanceMethods
并且shouldAutomaticallyForwardRotationMethods
也被实现并返回YES(对于那些使用iOS 6+的人);
我不确定在容器控制器的子视图控制器中处理方向变化的最佳方法是什么。