Skip to content

A UIView category for convenient NSLayoutConstraint creation using a simple formate language

Notifications You must be signed in to change notification settings

stepmuel/ConstraintFormat

Repository files navigation

ConstraintFormat

A UIView category for convenient NSLayoutConstraint creation using a simple format language.

This repository was created for my blog post which contains additional information.

Usage

UIView+ConstraintFormat enables the creation of NSLayoutConstraints using the format view1.attribute1 = multiplier * view2.attribute2 + constant.

NSDictionary *views = NSDictionaryOfVariableBindings(v1, v2);
[self addConstraintWithFormat:@"v1.top = v2.top" views:views];
[self addConstraintWithFormat:@"v1.bottom = v2.bottom" views:views];
[self addConstraintWithFormat:@"v1.left = v2.left" views:views];
[self addConstraintWithFormat:@"v1.right = v2.right" views:views];

The following attributes are supported:

NSDictionary *attributes = @{
    @"notAnAttribute" : @(NSLayoutAttributeNotAnAttribute),
    @"left" : @(NSLayoutAttributeLeft),
    @"right" : @(NSLayoutAttributeRight),
    @"top" : @(NSLayoutAttributeTop),
    @"bottom" : @(NSLayoutAttributeBottom),
    @"leading" : @(NSLayoutAttributeLeading),
    @"trailing" : @(NSLayoutAttributeTrailing),
    @"width" : @(NSLayoutAttributeWidth),
    @"height" : @(NSLayoutAttributeHeight),
    @"centerX" : @(NSLayoutAttributeCenterX),
    @"centerY" : @(NSLayoutAttributeCenterY),
    @"baseline" : @(NSLayoutAttributeBaseline)
};

More examples can be found in the included example project (ViewController.m).

About

A UIView category for convenient NSLayoutConstraint creation using a simple formate language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published