Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about the direction of iOS constraints

I've been working through Matt Nueberg's Programming iOS 7 book and am struggling to understand something with the direction of constraints. On page 28 of the book, in the multiplier, constant section, the author states:

The first attribute is set to the result.

So the way I read this, is that when there is a two item constraint, item 1 is derived or dependent based on the value of item 2.

So I have a view and a subview (button), I want to center the button horizontally. So I arrange my view and button and then control-drag from the button to the parent view:

enter image description here

and select Center Horizontally:

enter image description here

Then if I select that constraint though, I see the following in the attributes inspector at the right:

enter image description here

This shows the superview as item 1, and the button as item 2. Does this mean, that it would try to adjust the center of the superview to match the center of the button? That wouldn't be what I want. Or am I misunderstanding the meaning of item 1 and item 2? Or does it mean that I did the control-drag backwards, that you're supposed to drag from the dependency to the dependent, rather than the dependent to the dependency as I did?

I assume the order does matter, that is, which is item 1 and which is item 2. Because there's a menu option to swap the two. And if it were just a solver with no preference for what was adjusted, that wouldn't be necessary.

like image 738
Travis Griggs Avatar asked Mar 26 '14 23:03

Travis Griggs


People also ask

What are IOS constraints?

With constraints, you can say “these items are always lined up in a horizontal row” or “this item resizes itself to match the height of that item.” Constraints provide a layout language that you add to views to describe geometric relationships. The constraints you work with belong to the NSLayoutConstraint class.

What is constraints in IOS Swift?

Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints. This produces layouts that dynamically respond to both internal and external changes.

How do I change constraints in Xcode?

Clicking the Edit button in any of the constraints brings up a popover where you can change the constraint's relationship, constant, priority, or multiplier. To make additional changes, double-click the constraint to select it and open it in the Attribute inspector.


1 Answers

You did drag the association the correct way (and that's why the menu option listed centre in container).

The first item is the one that the constraint is applied to. When you want to centre a view in its container the constraint is applied to the container, not the subview.

Say that you were applying a constraint to link the top of a view to the bottom of the top layout guide. In this case the constraint is applied to the view, so it is item 1 and the layout guide is item 2.

The engine is just a solver, but the order matters in some cases. If 2 items are marked as equal then it doesn't matter which is first. But if the constraint has a multiplier then the order matters - like an aspect ratio constraint where the order dictates what is used as the width and what as the height.

like image 194
Wain Avatar answered Sep 30 '22 10:09

Wain