Sunday, February 28, 2016

iOS auto layout Visual Format Language

Auto Layout Visual Format Language to specify common constraints, including standard spacing and dimensions, vertical layout, and constraints with different priorities. In addition, this appendix contains a complete language grammar.

Standard Space
    [button]-[textField]

Width Constraint
    [button(>=50)]

Connection to Superview
    |-50-[purpleBox]-50-|

Vertical Layout
V:[topField]-10-[bottomField]

Flush Views
    [maroonView][blueView]

Priority
    [button(100@20)]

Equal Widths
    [button1(==button2)]

Multiple Predicates
    [flexibleButton(>=70,<=100)]

A Complete Line of Layout
    |-[find]-[findNext]-[findField(>=20)]-|



The notation prefers good visualization over completeness of expressibility. Most of the constraints that are useful in real user interfaces can be expressed using visual format syntax, but there are a few that cannot. One useful constraint that cannot be expressed is a fixed aspect ratio (for example, imageView.width = 2 * imageView.height). To create such a constraint, you must use 

constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.


If you make a syntactic mistake, an exception is thrown with a diagnostic message. For example:
    
    Expected ':' after 'V' to specify vertical arrangement
    V|[backgroundBox]|
    ^


References:

No comments:

Post a Comment