Home > Articles > Mobile Application Development & Programming

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

This chapter is from the book

Constraint Attributes

Constraints work with 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-7) speak to physical geometry. Constraints offer the following view attribute vocabulary:

  • left, right, top, and bottom—The edges of a view’s alignment rectangle on the left (A), 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.
  • 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).
    Figure 1.7

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

  • 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 views’ alignment rectangle.
  • baseline—The alignment rectangle’s baseline (I), typically a set offset above its bottom attribute.

The relation verbs 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:

  • Less-than-or-equal inequalityNSLayoutRelationLessThanOrEqual
  • EqualityNSLayoutRelationEqual
  • Greater-than-or-equal inequalityNSLayoutRelationGreaterThanOrEqual

This might not sound like a lot expressively. However, these three relations cover all the ground needed for specific equalities and for maximum and minimum limits.

  • + Share This
  • 🔖 Save To Your Account