I’m trying to use the UIViewController containment feature in one of my projects. The app is meant to be used in landscape mode only.
I add UIViewController A as a child of UIViewController B and add A‘s main view as a subview of one of B‘s views. I’m also saving a reference to A in B:
@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
The problem I have is that landscape orientation is not being respected. I did some debugging and found a solution, but I fear is not ideal as it’s more of a hack:
In 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];
}
In A:
- (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**);
}
Am I missing something here?
Your code:
was always wrong. You MUST send
didMoveToParentViewController:to the child controller after adding it to the parent. See my discussion here:http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers
As for the rotation, it is probable that you’re just doing all this too early. The app starts out in portrait and hasn’t rotated yet to landscape when
viewDidLoadis called. I give solutions to this problem here:http://www.apeth.com/iOSBook/ch19.html#_rotation
Note the suggestion there that you wait until
didRotateFromInterfaceOrientation:to finish setting up your view’s appearance. I think you might be facing here the same issues I’m describing there.