iOS Auto Layout Demystified(2nd)

Updating Constraints

When devices rotate and windows resize, view constraints may become invalid. You update constraints in the updateConstraints (UIView and NSView) and/or updateViewConstraints (UIViewController) methods. These implementations follow a certain flow:当设备旋转和调整窗口的大小view约束可能变的不合法。更新约束用updateConstraints或updateViewConstraints方法

  1. Call super (for example, [super updateViewConstraints]). Never forget this step.

  2. Clean away any invalid constraints. You may also remove valid constraints entangled with the invalid ones in order to start off with a clean slate for your updated layout.清除不合法的约束

  3. Add constraints to express your fresh layout.添加约束来表示新的布局。

- (void) updateViewConstraints 
{
// Always call super
[super updateViewConstraints];
// Remove constraints referencing exampleView
// These methods were introduced in Chapters 2 and 5
for (NSLayoutConstraint *constraint in
exampleView.referencingConstraintsInSuperviews) // C05 
[constraint remove]; // C02
// Re-establish position constraints using 
// self-explanatory layout macros
BOOL layoutIsPortrait =
UIDeviceOrientationIsPortrait(self.interfaceOrientation); 
if (layoutIsPortrait)
{
    ALIGN_CENTERTOP(exampleView, AQUA_INDENT); 
}
else 
{
    ALIGN_CENTERRIGHT(exampleView, AQUA_INDENT); 
}
}