Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

Constraint Attributes

Constraints use a limited geometric vocabulary. Attributes are the “nouns” of the constraint system, describing positions within a view’s alignment rectangle. Relations are “verbs,” specifying how the attributes compare to each other.

The attribute nouns (see Figure 1-5) speak to physical geometry. Constraints offer the following view attribute vocabulary:

Figure 1-5

Figure 1-5 Attributes specify geometric elements of a view.

  • Left, right, top, and bottom—The edges of a view’s alignment rectangle on the left (A in Figure 1-5), right (B), top (C), and bottom (D) of the view. These correspond to a view’s minimum X, maximum X, minimum Y, and maximum Y values. (The coordinate system used by UIKit and Auto Layout has its origin at the top-left.)
  • Leading and trailing—The leading and trailing edges of the view’s alignment rectangle. In left-to-right (English-like) systems, these correspond to “left” (leading, A) and “right” (trailing, B). In right-to-left linguistic environments like Arabic or Hebrew, these roles flip; right is leading (B), and left is trailing (A).
  • Width and height—The width (E) and height (F) of the view’s alignment rectangle.
  • CenterX and CenterY—The X-axis (H) and Y-axis (G) centers of the view’s alignment rectangle.
  • Baseline—The alignment rectangle’s baseline (I), typically a fixed offset above its bottom attribute.

Relations compare values. Constraint math is limited to three relations: setting equality or setting lower and upper bounds for comparison. You can use the following layout relations:

  • NSLayoutRelationLessThanOrEqual—For less-than-or-equal inequality
  • NSLayoutRelationEqual—For equality
  • NSLayoutRelationGreaterThanOrEqual—For greater-than-or-equal-to inequality

You might not think that these three relations would give you much to work with. However, these three relations cover all the ground needed for user interface layout. They offer ways to set specific values and apply maximum and minimum limits.

  • + Share This
  • 🔖 Save To Your Account