What is the correct way to do a constraint
with the constant 0 and leave a space in the margin of the view?
self.view.addConstraint(NSLayoutConstraint(item: gestureView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .LeadingMargin, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: gestureView, attribute: .LeadingMargin, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 0))
The margin, should it be applied to the first view or to the second?
Here is a way to do it with the result. The example is basically a square inside another.
Result
Still, I recommend you use the
Visual Format
as it is much shorter and easier to read. You will get the same result with the following constraints:To leave a margin between a view
parentView
and a sub-viewchildView
, the order ischildView
>parentView
(Margin):It results in:
It is better to use
attribute
with theMargin
suffix, and leave theconstant
a0
. We look at an example of why it is betterMargin
:It results in: