I have created an app in ios 8. In that I have 4 labels which are available as vertically (it should change the position for certain conditions), I have disabled auto layout and setting constraints programmatically. Now, the problem is however I can set constraints for horizontal and vertical positions, width and height, I could not find any way to add constraints for horizontal and vertical spacing between the labels.
Can anyone please help me to do this.
So far I have the following code:
NSArray *constraint_V_WebUrl = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[weburl(31)]" options:0 metrics:nil views:contentDictionary];
NSArray *constraint_H_WebUrl = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[weburl(196)]" options:0 metrics:nil views:contentDictionary];
NSArray *constraint_POS_H_WebUrl = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-13-[weburl]" options:0 metrics:nil views:contentDictionary];
[self.cardDetails addConstraints:constraint_POS_V_WebUrl];
[self.cardDetails addConstraints:constraint_POS_H_WebUrl];
[self.cardDetails addConstraint:[NSLayoutConstraint constraintWithItem:self.weburl attribute:NSla relatedBy:NSLayoutRelationEqual toItem:self.cardDetails attribute:NSLayoutAttributeTopMargin multiplier:1 constant:0.0]];
Code above works fine. But I want to set horizontal and vertical spacing between the labels.
You can use both visual format and code format for defining constraints. Visual format has many restrictions and not applicable to all cases.
In your case you can use (horizontal spacing):
[NSLayoutConstraint constraintWithItem:YourViewAtLeft
attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:YourViewAtRight attribute:NSLayoutAttributeLeft
multiplier:1.0 constant:ValueOfSpacing];
// Paste your view names and constraint value.
And then add this constraint with addCostraint
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With