Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Child View Controller and Auto Layout

I have a very simple scenario that is not working for me. I have one storyboard with two view controllers:

ViewController1
- ViewA
- ViewB

ViewController2
- ViewC

My ViewController1 is working as intended and when I rotate the device the views adjust correctly according to the auto layout that I defined.

My ViewController2 have a Freeform size (768x722) and I unchecked the option "Resize View From Nib".

This is my ViewController1 didLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    UIViewController *vc2 = [story instantiateViewControllerWithIdentifier:@"vc2"];

    [self addChildViewController:vc2];
    [vc2 didMoveToParentViewController:self];
    [self.viewA addSubview:vc2.view];
}

When I rotate the device the method - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation from ViewControlled2 its called and it shows the correct orientation. However, ViewC does not resize it self and does not follow my Auto Layout settings.

Anyone know how I can fix? I just want to add my ViewController2 inside ViewA and keep the Auto Layout working for views of ViewController2. I need a solution for iOS 7 only, so no compatibility with earlier versions is needed.

Anyone can help me?

like image 435
Rafael Avatar asked Mar 21 '23 22:03

Rafael


1 Answers

I found the solution and I will register it here, just in case someone needs it.

I created an UIViewController property to hold my ViewController2. After that, I added the following method:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    self.vc2.view.frame = self.viewA.bounds;
}
like image 199
Rafael Avatar answered Apr 01 '23 12:04

Rafael