0

I faced with strange behaviour of auto layout and storyboard. I have view controller in storyboard with child view (let's say panel) and I have following constraints: height = 500, width = 300 and centreX = parent.centreX added with Interface Builder.

When I run following piece of code I see two different values and I don't understand how additional +50 points appeared:

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (NSLayoutConstraint *constraint in _view.constraints)
    {
        NSLog(@"did load: %@", constraint);
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    for (NSLayoutConstraint *constraint in _view.constraints)
    {
        NSLog(@"did appear: %@", constraint);
    }
}

When view did load I see height constraint with value = 500 but when view controller did appear I see height constraint with value = 550. Constraint objects are the same so it looks like UIKit updated height constraint value for some reason. Also I don't see any warnings or errors in console.

John Tracid
  • 3,836
  • 3
  • 22
  • 33
  • This is likely because the views are only constructed in viewDidLoad and are not yet sized and presented on the UI until viewDidAppear http://stackoverflow.com/questions/11254697/difference-between-viewdidload-and-viewdidappear – Dave S Feb 24 '16 at 18:21
  • I understand this but I don't understand why I'm getting additional 50 points. I was thinking that if I set height constraint, then I have it permanent. – John Tracid Feb 24 '16 at 18:47
  • What you're likely seeing is the view is constructed to some default specification or initial specification, then in onViewDidAppear the layout constraints are applied and the views are sized to fit the screen. – Dave S Feb 24 '16 at 18:48
  • No, because I set 500 points for height constraint in storyboard. This is not a default spec. – John Tracid Feb 24 '16 at 18:50
  • If you have it set to scale vertically and you run it on a larger phone it may increase 50 points to fill the space. Does it do this for every screen size? The initial spec I was speaking about would be the 500 you set in the storyboard but layout constraints can override that initial height – Dave S Feb 24 '16 at 18:51

0 Answers0