3

While working with Auto Layout and Constraints in the Interface Builder, I noticed that I am getting different results with the same constraints when I run iOS7 vs iOS8 simulator.

For example, I have a very simple Trailing Constraint set on a UIButton of 8pts. These 8pts look very different in placement when running on iOS7 vs iOS8 simulator. iOS8 simulator seems to add a few points to the constraint. It's as if there needs to be functionality for Constraint Delta's...

Any ideas why constraints behave different on iOS7 vs iOS8?

iOS 8 iOS 7 Interface Builder

MiMo
  • 4,905
  • 3
  • 22
  • 23

1 Answers1

7

The reason is that iOS 8 has layout margins and iOS 7 does not. You have made your constraints between the edge of the label and the margin of the superview — but in iOS 7 there are no margins. Thus, in order to be compatible with both, the runtime must do something. So it treats the margins as zero in iOS 7, which is what they are because they don't exist at all.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for that information Matt, so what is the common practice to accommodate this difference between the iOS7 and iOS8? – MiMo Dec 29 '14 at 21:04
  • 1
    I don't know what you _want_ to do to "accommodate the difference". What _I_ would do is make my constraint to the _edge_ of the superview, not the _margin_ of the superview, so that it works the same on both iOS 8 and iOS 7. – matt Dec 29 '14 at 21:11
  • 1
    Matt, I've done just that, if you look above, I've added a screenshot setting of my constraint. You can see that it's not trailing to the margins, but to the superview edge as you recommend. – MiMo Dec 29 '14 at 21:14
  • 1
    By the way, in your case the problem is compounded by an intervening view. In other words, it is Superview's constraints that have the problem. Label is constrained to the edge of Superview, but Superview is constrained to _its_ superview's margins. – matt Dec 29 '14 at 21:14
  • 1
    Yes, but there is more to your view hierarchy than you are revealing. – matt Dec 29 '14 at 21:15
  • Thanks Matt! It turns out that my superview had constraints that were related to another view's margins... Fixing the superview fixed the rest, thank you. Im assuming Interface builder adds constraints related to margins by default, since they are present in iOS8. – MiMo Dec 29 '14 at 21:21