Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Concise Auto Layout macros
#define Left NSLayoutAttributeLeft
#define Right NSLayoutAttributeRight
#define Top NSLayoutAttributeTop
#define Bottom NSLayoutAttributeBottom
#define Leading NSLayoutAttributeLeading
#define Trailing NSLayoutAttributeTrailing
#define Width NSLayoutAttributeWidth
#define Height NSLayoutAttributeHeight
#define CenterX NSLayoutAttributeCenterX
#define CenterY NSLayoutAttributeCenterY
#define Baseline NSLayoutAttributeBaseline
#define LessThanOrEqual NSLayoutRelationLessThanOrEqual
#define Equal NSLayoutRelationEqual
#define GreaterThanOrEqual NSLayoutRelationGreaterThanOrEqual
#define Constraint(item1, attr1, rel, item2, attr2, con) [NSLayoutConstraint constraintWithItem:(item1) attribute:(attr1) relatedBy:(rel) toItem:(item2) attribute:(attr2) multiplier:1 constant:(con)]
#define VisualConstraints(format, ...) [NSLayoutConstraint constraintsWithVisualFormat:(format) options:0 metrics:nil views:_NSDictionaryOfVariableBindings(@"" # __VA_ARGS__, __VA_ARGS__, nil)]
#define VisualConstraintWithMetrics(format, theMetrics, ...) [NSLayoutConstraint constraintsWithVisualFormat:(format) options:0 metrics:(theMetrics) views:_NSDictionaryOfVariableBindings(@"" # __VA_ARGS__, __VA_ARGS__, nil)]
#define ConstantConstraint(item, attr, rel, con) Constraint((item), (attr), (rel), nil, NSLayoutAttributeNotAnAttribute, (con))
#define horizontallyFillSuperview ^(UIView *view, NSUInteger idx, BOOL *stop) {[view.superview addConstraints:VisualConstraints(@"|[view]|", view)];}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment