iOS 7 vs iOS 8 Constraints differ in sizes

2019-07-06 16:35发布

问题:

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?

回答1:

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.