Issue with AutoLayout and ChildViewControllers (incorrect size of ChildVCs' view)
Asked Answered
P

1

6

I'm experiencing some difficulties doing a rather simple thing, I'm missing something but don't see...

I reproduced the issue with a very simple App (using IB) :

  • App's main ViewController is a UINavigationController.
  • NavigationController's root is "FirstViewController".
  • FirstViewController and SecondViewController are empty UIViewController subclasses.
    • Their XIB files where generated by XCode when creating the classes, AutoLayout is enabled.
    • I placed Labels on top and bottom of SecondViewController (Vertical space constraints = 0).

Using ChildViewControllers

Problem is if I display SecondViewController via "ChildViewControllers" method, it goes wrong on my iPhone4: I don't see bottom label.

// In FirstViewController.m
- (IBAction)child:(id)sender {
    [self addChildViewController:self.secondVC];
    [self.view addSubview:self.secondVC.view];
    [self.secondVC didMoveToParentViewController:self];
}

Using NavigationController

If I display "SecondViewController" through the NavigationController, everything is fine, SecondViewController is displayed properly.

// In FirstViewController.m
- (IBAction)push:(id)sender {
    [self.navigationController pushViewController:self.secondVC animated:YES];
}

Also, as soon as SecondViewController has been displayed once through NavigationController, it'll be always well displayed.

I'm surely missing something, but what? :p Do you have any ideas?

I Uploaded the simple project on dropbox: https://dl.dropbox.com/u/36803737/sharebox/AutoLayoutTest.zip

Thanks!

julien

Prakrit answered 5/1, 2013 at 15:59 Comment(2)
Screenshots FirstViewController: dl.dropbox.com/u/36803737/sharebox/AutoLayout/photo%202.PNG SecondViewController OK: dl.dropbox.com/u/36803737/sharebox/AutoLayout/photo%203.PNG SecondViewController NOK: dl.dropbox.com/u/36803737/sharebox/AutoLayout/photo%201.PNGPrakrit
It seems to be OK if I manually specify view's frame, but it's not a solution for me: self.secondVC.view.frame = self.view.frame; in child method. What do you think about that?Prakrit
T
28

Your dropbox link doesn't work, so I couldn't try this out. Try setting the frame of secondVC before you add it as a subview:

secondVC.view.frame = self.view.bounds;

If you want to do it with constraints, I do it this way:

- (IBAction)child:(id)sender {
    [self addChildViewController:self.secondVC];
    [self.view addSubview:self.secondVC.view];
    [self constrainViewEqual:secondVC.view];
    [self.secondVC didMoveToParentViewController:self];
}

-(void)constrainViewEqual:(UIView *) view {
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    NSArray *constraints = @[con1,con2,con3,con4];
    [self.view addConstraints:constraints];
}

Since I use constraints fairly often, I have the above method (and others) in a categorry on UIView to keep my code looking cleaner.

Tumor answered 5/1, 2013 at 16:44 Comment(3)
@jrturton, mine only has a couple of methods at this point, mostly because I keep changing the few methods I have to suit the task at hand, rather than adding new ones -- I should stop that, and keep adding methods that I find broadly useful.Tumor
Setting the frame did the trick! Didn't seem intuitive for me but why not :-) Thanks also for your constraint-based answer!Prakrit
and after one day effort i eventually found this post which saved my further time to be wasted.Resupinate

© 2022 - 2024 — McMap. All rights reserved.