Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS7: Calling -makeConstraints: order seems to matter #28

Closed
BigAB opened this issue Nov 25, 2013 · 2 comments
Closed

iOS7: Calling -makeConstraints: order seems to matter #28

BigAB opened this issue Nov 25, 2013 · 2 comments

Comments

@BigAB
Copy link

BigAB commented Nov 25, 2013

iOS7 Bug?

Maybe it is my misunderstanding about how AutoLayout or Masonry work, but it seems to me like the order of calling -makeContraints: in iOS7 is somehow order dependant.

Here is a very simple example:

Expected Result

A very simple view, with a yellow view on top and a blue view on bottom:
Expected Result (Gist)

screenshot 2013 11 25 10 29 00

[blueView makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(yellowView.bottom).offset(padding);
    make.bottom.equalTo(superview.bottom).offset(-padding);
    make.left.equalTo(superview.left).offset(padding);
    make.right.equalTo(superview.right).offset(-padding);
  }];

[yellowView makeConstraints:^(MASConstraintMaker *make) {
    make.top.greaterThanOrEqualTo(superview.top).offset(padding);
    make.left.equalTo(superview.left).offset(padding);
    make.right.equalTo(superview.right).offset(-padding);
    make.height.equalTo(@100);
}];

Possible Bug

All I do is switch the order -makeContraints: is called, calling yellowView first :
Possible Bug Result (Gist)

screenshot 2013 11 25 10 05 13

[yellowView makeConstraints:^(MASConstraintMaker *make) {
    make.top.greaterThanOrEqualTo(superview.top).offset(padding);
    make.left.equalTo(superview.left).offset(padding);
    make.right.equalTo(superview.right).offset(-padding);
    make.height.equalTo(@100);
}]

[blueView makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(yellowView.bottom).offset(padding);
    make.bottom.equalTo(superview.bottom).offset(-padding);
    make.left.equalTo(superview.left).offset(padding);
    make.right.equalTo(superview.right).offset(-padding);
}];

On iOS6 this seems to work fine.
So is this a bug or am I missing something?

@cloudkite
Copy link
Contributor

Firstly thanks for the detailed issue!

It's probably due to a implementation change in how iOS7 handles ambiguous constraints, do you get any console messages?.
I think your problem lies in the fact you are using make.top.greaterThanOrEqualTo(superview.top).offset(padding);
have you tried using make.top.equalTo(superview.top).offset(padding); instead?

By using make.top.greaterThanOrEqualTo(superview.top).offset(padding); you are basically telling Auto Layout this view can be anywhere vertically as long as it's more than padding.

greaterThanOrEqualTo is best used if you have more than one constraint on that NSLayoutAttribute otherwise Auto Layout may treat it as ambiguous which has weird results.

Let me know if that helps

@BigAB
Copy link
Author

BigAB commented Nov 26, 2013

There are no console messages.

It may be ambiguous as, you're right, changing it to equalTo does seem to fix the issue.

I did find it odd that it worked in iOS6 but not iOS7, but as you said, maybe they just handle ambiguous layout differently.

Thanks for your time.

@BigAB BigAB closed this as completed Nov 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants