gpt4 book ai didi

ios - 未调用 subview Controller 旋转方法

转载 作者:技术小花猫 更新时间:2023-10-29 11:03:33 25 4
gpt4 key购买 nike

总结

我正在尝试将 subview Controller 添加到父 View Controller ,并让父 View Controller 通知 subview Controller 旋转事件。但是,旋转消息不会被转发到 subview Controller (这是默认行为)。为什么没有发生这种默认行为?

环境:iOS 7、XCode 5、OSX 10.9

详情:

我正在按照 Apple 文档中的说明实现自定义容器 View Controller :"Creating Custom Container View Controllers" .我正在尝试建立一个简单的父子关系来转发轮换事件。总体层次结构与此处重绘的文档图 14-1 中的图片完全相同:

ParentViewController --------> RootView
| / \
| / \
˅ / \
ChildViewController ---> ChildView \
\
OverLayView

我在 parentViewController(unityViewController = childViewController,unityView = childView)中使用文档 list 4-1 中的代码来完成此操作:

// In the ParentViewController    
// Called by the Application Delegate in application:didFinishLaunchingWithOptions:
- (void)addUnityViewController:(UIViewController *)unityViewController withUnityView:(UIView *)unityView
{
[self addChildViewController:unityViewController]; // 1. Establish Child parent relationship

unityView.frame = self.view.frame; // 2. Set the frame (explicitly or with constraints)
[self.view addSubview:unityView]; // 2.1 Add the subview AFTER you set the frame
... // add some view constraints here

[self.view sendSubviewToBack:unityView]; // In the back, but not completely obstructed by any other views
[unityViewController didMoveToParentViewController:self];// 3. Tell the child what happened
}

此代码成功地将 subview 显示为 RootView 的 subview ,并且 OverlayView 添加了一些功能按钮。但是,当设备旋转时,父 View Controller 成功旋转了它的 View ,但没有将旋转消息转发给 subview Controller (unityViewController),导致 rootView 中的 subview (unityView)显示不正确。根据"Creating Custom Container View Controllers"这应该自动发生:

Customizing Appearance and Rotation Callback Behavior: Once you add a child to a container, the container automatically forwards rotation and appearance callbacks to the child view controllers as soon as an event occurs that requires the message to be forwarded.

为了确保这应该发生,我覆盖了以下方法:

// In the parent ViewController:
- (BOOL)shouldAutorotate
{
return YES;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods
{
return YES;
}

- (BOOL)shouldAutomaticallyForwardRotationMethods
{
return YES;
}

但是,永远不会调用 child viewController (unityViewController) 中的 willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation: 方法。我已经能够确认确实调用了 parent 方法,并且在那些方法中我调用了 super,但从未调用过子方法。

问题:

没有发生此默认行为的原因是什么?我需要父项和子项都接收轮换消息以正确显示。

注意:我知道我可以在父级中手动调用这些方法,但我不想手动调用这些方法并添加默认行为的额外代码。

非常感谢您的帮助!

最佳答案

为了让您的 subview Controller 接收转发的方向回调,它必须是可见的。您的 subview Controller 可能不可见,因为它的 View 尚未添加到 View 层次结构中。

如果你改变你的方法,你应该开始看到你的旋转消息被调用:

- (void)addUnityViewController:(UIViewController *)unityViewController 
{
[self addChildViewController:unityViewController];

unityViewController.view.frame = self.view.bounds;
[self.view addSubview:unityViewController.view];
... // add some view constraints here

[self.view sendSubviewToBack:unityViewController.view];
[unityViewController didMoveToParentViewController:self];
}

更多信息:Responding to Orientation Changes in a Visible View Controller .

关于ios - 未调用 subview Controller 旋转方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22719833/

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