Adding a view controller's view as a subview changes it's frame?
SomeViewController *someVC = [[SomeViewController alloc] initWithNibName:@"SomeViewController" bundle:nil];
self.someViewController = someVC;
[someVC release];
NSLog(@"height before added as subview: %f", self.someViewController.view.frame.size.height);
[self.containerView addSubview:self.someViewController.view];
NSLog(@"height AFTER added as subview: %f", self.someViewController.view.frame.size.height);
// height before added as subview: 480.000000
// height AFTER added as subview: 540.开发者_如何学JAVA000000
What possible explanation is there for this? someViewController's view does not have any autoresizing properties set, and containerView does not autoresize it's subviews. What could cause the frame to change by just adding it as a subview?
A further mystery to this is that it only happens when someViewController's view height is >= 480.
What kind of view controller is SomeViewController
? The one thing I think that may be happening is SomeViewController
may contain some extra content such as the top bar of an UINavigationController
and that is automatically increasing the size sometimes. But what you are doing is violating the HIG by adding the view of one view controller to another view controller. An UIViewController
is designed to be the full view of the screen or belonging to a specialized view controller such as an UITabBarController
, UINavigationController
, or UISplitViewController
. As a result someViewController
will not function as expected and not receive calls such as viewDidLoad
. So if you are sure that no autoresizing properties are set all I can recommend is using an UIView
instead of an UIViewController
.
This can be caused by the autoresizingMask of the view, it's purpose is to adjust the size to changes in it's super view.
精彩评论