I tried to get as close as possible to Apple's implementation, recognizing the existence of NSLayoutConstraint , so I just defined a set of Compressed Auto Layout Macros that remove prefixes from attributes and relationships and complete the creation of constraints in the macro sequence of the constructor functions (also omitting the multiplier parameter that I never use) :
ConstraintConstantConstraintVisualConstraintsVisualConstraintWithMetrics
To make some simple restrictions on viewing, I end them in an array literal. I always prefix non-zero constants with a sign to emphasize the offset. In practice, it looks like this (instance variables refer to views):
[self.view addConstraints: @[Constraint(_verticalSeparator, CenterX, Equal, _top, CenterX, 0), Constraint(_verticalSeparator, CenterY, Equal, _top, CenterY, +22), Constraint(_verticalSeparator, Height, Equal, _localWeather, Height, 0), Constraint(_localWeather, CenterY, Equal, _verticalSeparator, CenterY, 0), Constraint(_addLocation, CenterY, Equal, _verticalSeparator, CenterY, 0), Constraint(_touchDown, Trailing, Equal, _verticalSeparator, Trailing, -1), Constraint(_touchDown, CenterY, Equal, _localWeather, CenterY, 0), Constraint(_touchDown, Width, Equal, _localWeather, Width, +26), Constraint(_touchDown, Height, Equal, _localWeather, Height, 0) ]]; id f = @"[_localWeather]-space-[_verticalSeparator]-space-[_addLocation]"; [self.view addConstraints: VisualConstraintWithMetrics(f, @{@"space": @11}, _localWeather, _verticalSeparator, _addLocation)]; [_tableCell addConstraint:ConstantConstraint(_tableCell, Height, Equal, 44)];
When working with a group of views in which everyone has the same setting, I list the array literal as follows:
[@[_top, _middle, _bottom, _touchDown, _verticalSeparator] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) { view.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:view]; }];
Filling a superview horizontally happened often enough to be lifted into its own macro:
[@[_top, _middle, _bottom] enumerateObjectsUsingBlock:horizontallyFillSuperview];
I will update this answer when my style evolves ...