I have an UI element (UISwitch
actually, but does not matter really) that has both leading and trailing space pinned to superview in Interface Builder. The constraint looks like this in Xcode 6:
The constraint for leading space is the same effectively. The value of constraint is 42.0 points.
This is exactly what I want, because for different devices I can change layoutMargins
property on UIView
and the constraints will work correctly, to increase margin between views.
Now I want to add another view in code that would also have both leading and trailing space pinned to it's superview margin, so the same layoutMargins
set to superview will work.
I pinned the view using visual format language with the following syntax:
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-42.0-[separatorView]-42.0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.contentView, separatorView)];
[self.contentView addConstraints:constraints];
[self.contentView setNeedsUpdateConstraints];
This works, but layoutMargins
property has no effect using this constraint, so it is obviously not pinned to margin, but directly to superview.
So my question is:
How to pin UI element spaces to margin in code using visual format language? Or if not possible, how to pin with constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
API?