2011-08-24 47 views
1

我有两个UIViewControllers,UIViewControllerParent和UIViewControllerChild。里面UIViewControllerParent我实例化一个孩子控制器并添加其观点,作为一个子视图:如何处理子视图的方向更改

UIViewControllerChild *child = [[UIViewControllerChild alloc] init]; 
[self.view addSubView:child.view]; 

现在,当方向的变化,只有父viewcontrollers shouldAutorotateToInterfaceOrientation方法被调用,只有父母取向的变化。儿童的AutorotateToInterfaceOrientation方法没有被调用并且它的视图不能旋转是否是正确的行为?

如果是这样,获取子视图旋转的最佳实践方法是什么?应该有办法让孩子控制器自动布局,我只是不知道如何。

有什么建议吗?

回答

4

只有父viewcontrollers shouldAutorotateToInterfaceOrientation方法被调用

这是正常的。父视图的控制器负责确定支持哪些方向。从Apple

注意:您可以添加一个视图控制器的财产的UIView作为一个子视图 到另一个视图控制器。在这样做时,两个视图都将旋转,但父视图控制器仍然负责通过其shouldAutorotateToInterfaceOrientation 方法确定支持的方向。

其他几件你要检查的东西也在该链接中详细说明。

+0

只是为了清楚我的问题到底是什么。我在窗口中添加了我的视图作为子视图,而不是在视图上。 – Craigt

2

这里是由苹果公司提供了一个项目,以表示将方向改变不同XIBs涉及:https://developer.apple.com/library/ios/#samplecode/AlternateViews/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008755

否则,如果你想要把一个子视图到前面,当方向的变化,使用此代码:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 

     [self.view bringSubviewToFront:portraitView]; 

    } else { 

     [self.view bringSubviewToFront:landscapeView]; 

    } 
} 

请记住在头文件中定义一个新的UIView并将其链接到XIB文件中。

0

最好不要在ViewController中创建ViewController。

在您的情况下,您可以将子ViewController的视图的引用添加到根ViewController。并保留由根ViewController管理的所有内容。