How to handle orientation change for subviews
I have two UIViewControllers, UIViewControllerParent and UIViewControllerChild. Inside UIViewControllerParent I instantiate a child controller and add its view as a subview:
UIViewControllerChild *child = [[UIViewControllerChild alloc] init];
[self.view addSubView:child.view];
Now when the orientation changes, only the parent viewcontrollers shouldAutorotateToInterfaceOrientation method gets called and only the parents orientation changes. Is it the correct behavior that the childs shouldAutorotateToInterfaceOrientation method does not get called and its view not to rotate?
If so, wh开发者_StackOverflow社区at is the best practice method for getting the child view to rotate? There should be a way to ge the child controller to layout automatically, I just dont know how.
Any advice?
only the parent viewcontrollers shouldAutorotateToInterfaceOrientation method gets called
This is normal. The parent view's controller is responsible for determining what orientations are supported. From Apple:
Note: You may add one view controller's UIView property as a subview to another view controller. In doing so, both views will rotate but the parent view controller remains in charge of determining the supported orientations via its shouldAutorotateToInterfaceOrientation method.
Several other things for you to check are also detailed at that link.
Here is a project provided by Apple that deals with showing different XIBs when the orientation changes: https://developer.apple.com/library/ios/#samplecode/AlternateViews/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008755
Otherwise, if you want to bring a subview to the front when the orientation changes, use this code:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self.view bringSubviewToFront:portraitView];
} else {
[self.view bringSubviewToFront:landscapeView];
}
}
Remember to define a new UIView in your header file and link it in the XIB file.
It's better not to create ViewControllers inside ViewController.
In your situation, you can add a reference of child ViewController's view to root ViewController. and leave everything managed by the root ViewController.
精彩评论