Create a gist now

Instantly share code, notes, and snippets.

@kwigbo /SKCoreLayout Secret
Last active Dec 30, 2015

What would you like to do?
This is a snippet to show the difference between creating constraints with SKCore and creating constraints with the methods provided by Apple.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -- Used By Both
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NSDictionary *metrics = @{@"padding" : @(10.0),
@"headerHeight" : @(30.0),
@"rowHeight" : @(50.0),
@"boxSize" : @(110.0)};
NSDictionary *views = NSDictionaryOfVariableBindings(view1, view2, view3, view4, view5);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -- SKCore Way
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[self beginConstraintsWithMetrics:metrics forViews:views];
[self addVFL:@"H:|-padding-[view1]-padding-|"];
[self addVFL:@"H:|-padding-[view5(==boxSize)]"];
[self addVFL:@"H:[view5]-padding-[view2]-padding-|"];
[self addVFL:@"H:[view5]-padding-[view3]-padding-|"];
[self addVFL:@"H:|-padding-[view4]-padding-|"];
// -- Vertical Layout
[self addVFL:@"V:[view1]-padding-[view5(==boxSize)]"];
[self addVFL:@"V:|-padding-[view1(==headerHeight)]-padding-[view2(==rowHeight)]-padding-[view3(==rowHeight)]-padding-[view4]-padding-|"];
[self.view addConstraints:[self endConstraints]];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -- Default Way
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NSMutableArray *array = [NSMutableArray array];
[array addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-padding-[view1]-padding-|"
options:0
metrics:metrics
views:views]];
[array addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-padding-[view5(==boxSize)]"
options:0
metrics:metrics
views:views]];
[array addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:[view5]-padding-[view2]-padding-|"
options:0
metrics:metrics
views:views]];
[array addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:[view5]-padding-[view3]-padding-|"
options:0
metrics:metrics
views:views]];
[array addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-padding-[view4]-padding-|"
options:0
metrics:metrics
views:views]];
// -- Vertical Layout
[array addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[view1]-padding-[view5(==boxSize)]"
options:0
metrics:metrics
views:views]];
[array addObjectsFromArray:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-padding-[view1(==headerHeight)]-padding-[view2(==rowHeight)]-padding-[view3(==rowHeight)]-padding-[view4]-padding-|"
options:0
metrics:metrics
views:views]];
[self.view addConstraints:array];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment