UIViewController包含

Ped*_*eno 2 cocoa-touch uiviewcontroller ios ios5

我正在尝试在我的一个项目中使用UIViewController包含功能.该应用程序仅用于横向模式.

我将UIViewController A添加为UIViewController B的子节点,并将A的主视图添加为B视图之一的子视图.我也在B中保存对A的引用:

@interface BViewController : UIViewController

@property (retain, nonatomic) AViewController *aVC;

@end

@implementation BViewController : UIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"];
    [self addChildViewController:self.aVC];
    [self.myContainerView addSubview:self.aVC.view];
}

@end
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是横向不被尊重.我做了一些调试并找到了解决方案,但我担心这并不理想,因为它更像是一个黑客攻击:

在B:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"];
    [self addChildViewController:self.aVC];
    [self.myContainerView addSubview:self.aVC.view];
    [self.aVC didMoveToParentViewController:self];
}
Run Code Online (Sandbox Code Playgroud)

在一个:

- (void)didMoveToParentViewController:(UIViewController *)parentVC
{
    // Interchange width and height
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.**height**, self.view.frame.size.**width**);
}
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?

mat*_*att 6

你的代码:

self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"];
[self addChildViewController:self.aVC];
[self.myContainerView addSubview:self.aVC.view];
Run Code Online (Sandbox Code Playgroud)

总是错的.您必须didMoveToParentViewController:在将其添加到父控制器后发送给子控制器.看我在这里的讨论:

http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers

至于轮换,很可能你太早做了这一切.该应用程序以纵向方式开始,并且在viewDidLoad调用时尚未旋转到横向.我在这里解决这个问题:

http://www.apeth.com/iOSBook/ch19.html#_rotation

请注意那里的建议,您要等到didRotateFromInterfaceOrientation:设置视图的外观.我想你可能会面临我在那里描述的同样问题.