Short Auto Layout Tips


WHY Auto Layout? TO calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views, reposition these views on changes... { device rotates (iOS), supports internationalization, ...}

3 Ways to set auto layout programmatically

Layout Constraint
Layout Anchors
Visual Format Language

The Visual Format Language lets you use ASCII-art like strings to define your constraints.


AutAuto Layout Errors

  • Ambiguous Layouts

Ambiguous layouts occur when the system of constraints has two or more valid solutions. There are two main causes:

  1. The layout needs additional constraints to uniquely specify the position and location of every view.
  2. The layout has conflicting optional constraints with the same priority, and the system does not know which constraint it should break.

Here, you need to tell the system which constraint it should break

  • Unsatisfiable Layouts

Unsatisfiable layouts occur when the system cannot find a valid solution for the current set of constraints. Two or more required constraints conflict because they cannot all be true at the same time


Change Layout at Runtime

Change Constraints{ update constarins}, or UIFrame {view will layout subviews}

Update Pass

The system traverses the view hierarchy and calls the updateViewConstraints method on all view controllers, and the updateConstraints method on all views. You can override these methods to optimize changes to your constraints

To batch a change, instead of making the change directly, call the setNeedsUpdateConstraints method on the view holding the constraint. Then, override the view’s updateConstraints method to modify the affected constraints.

If possible, use constraints to define all of your layouts. The resulting layouts are more robust and easier to debug. You should only override the viewWillLayoutSubviews or layoutSubviews methods when you need to create a layout that cannot be expressed with constraints alone.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了