I'm creating a view controller like this:
MyViewController* viewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
viewController.view.frame = CGRectMake(0,0,320,300);
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
Obviously I want the view to be sized to 0,0,320,300.
However, viewDidLoad gets called before the frame gets set, and thus in viewDidLoad I get this:
<UIView: 0x155d3180; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x155c20c0>>
So the views in the new view controller get laid out based on a height of 568 instead of the 300 that I want. What can I do to prevent this new view from being created with the wrong frame size?
Your calling of
viewController.view
(followed by.frame
) is what triggers the call toviewDidLoad
.The proper solution is to layout the subviews in the
viewWillLayoutSubviews
method of your view controller, not inviewDidLoad
.