Kodeco Forums

Auto Layout Visual Format Language Tutorial

Learn how to use Apple's Visual Format Language to create simple and complex Auto Layout constraints in your code, in this iOS tutorial.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1482-auto-layout-visual-format-language-tutorial

How to create the equivalent of

[View1 addConstraint:[NSLayoutConstraint constraintWithItem:View2
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:View1
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0.0]];
in Visual Format language .
Is it even possible where View1 might not be immediate parent of View2 ?
This is what I did :
long spacing = 0 - View1.frame.size.width;
constraintString = [NSString stringWithFormat:@โ€œH:[View1]-(%ld)-[View2]โ€,spacing];

where spacing is negative in this case . But it doesnโ€™t always work, What mistake am I doing ?

From what i see in your initial constraint you are trying to make the left edge of View2 0.0 to the left edge of View1.
Assuming the views are laid out vertically in the Visual Format Language you could do the following:

NSLayoutConstraint.constraintsWithVisualFormat(โ€œV:[view1]-[view2]โ€, options: .AlignAllLeft, metrics: nil, views: [โ€œview1โ€: view1, โ€œview2โ€: view2])

This will in a vertical layout make the view1 left and view2 left the exact same provided you had some horizontal guide on at least on of those two views.

If this is not what you were referring too please let me know so i can try and assist further.

I am getting uncaught exception even after activating the constraints . Kindly help me. - Christina

This needs to be updated for compatibility with Xcode 9

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]