Ronnie Liew
Ronnie Liew

Reputation: 18270

ViewController Containment - Child ViewController not sizing correctly in Landscape

The app is set to only support landscape mode.

The custom UIViewController has the following code:

- (void)viewDidLoad {
    [super viewDidLoad];

    MBMainViewController *mainViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MBMainViewController"];

    [self addChildViewController:mainViewController];
    [self.view addSubview:mainViewController.view];
    [mainViewController didMoveToParentViewController:self];

    self.mainViewController = mainViewController;
}

When app is launched, the frame for the view of the child viewcontroller (mainViewController) is still set to the dimension of a portrait.

Is there something I'm missing so the right frame size is set on the child viewcontroller's view?

Upvotes: 0

Views: 312

Answers (1)

Ronnie Liew
Ronnie Liew

Reputation: 18270

This apparently only happens to UIViewController that are instantiated from storyboard. In this case, it only works if we set the autoresizingMask of the view belonging to the child view controller:

- (void)viewDidLoad {
    [super viewDidLoad];

    MBMainViewController *mainViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MBMainViewController"];

// Set the autoresizingMask as a fix
    mainViewController.view.autoresizingMask = UIViewAutoResizingFlexibleWidth | UIViewAutoResizingFlexibleHeight;


    [self addChildViewController:mainViewController];
    [self.view addSubview:mainViewController.view];
    [mainViewController didMoveToParentViewController:self];

    self.mainViewController = mainViewController;
}

My guess is that when you instantiate a view controller from the storyboard, the constraints have already been set or that it doesn't come with the right autoresizingMask values.

Upvotes: 1

Related Questions