Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add horizontal spacing and vertical spacing programmatically in ios?

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.

like image 664
Balaji Kondalrayal Avatar asked Dec 19 '22 08:12

Balaji Kondalrayal


1 Answers

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.

like image 74
Egor Avatar answered Dec 24 '22 02:12

Egor