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

I created an app in ios 8. With this I have 4 labels that are vertically accessible (this should change position for certain conditions), I automatically disabled auto-layout and installation constraints. However, the problem is that I can set constraints for horizontal and vertical positions, width and height, I could not find a way to add constraints for horizontal and vertical spacing between labels.

Can anyone 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]];

      

The code above works fine. But I want to set the horizontal and vertical distance between the labels.

+3


source to share


3 answers


You can use both visual format and code format to define constraints. The visual format has many limitations and is not applicable in 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.

      

Then add this constraint with addCostraint

.

+4


source


You can use constraint to place a peep at a fixed distance (horizontal or vertical) from another. Just remember that when constraining two sibling views, the constraint must be added to the parent view.

Example: Let's say you have 2 labels, label1 and label2, both UIView children called parentView. Create a constraint with label2 and label1 as elements, label2 will remain equal to label1 on the right, multiplier 1 and constant equal to the desired horizontal spacing. Then add the constraint to the parentView, not any of the labels.



Sample code:

 [ parentView addConstraint:[NSLayoutConstraint constraintWithItem: label2 
                                                         attribute: NSLayoutAttribueLeft                                 
                                                         relatedBy: NSLayoutRelationEqual
                                                            toItem: label1  
                                                         attribute: NSLayoutAttributeRight 
                                                        multiplier: 1 
                                                          constant: LABEL_SPACING ] ];

      

+3


source


Balaji

I had a similar problem, but was able to fix it by creating constraints in the storyboard between labels and binding them (constraints) to the IBOutlet NSLayoutConstraints. By testing different screen sizes, the constraints can be changed in code.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint* verticalSpace1;

      

And then, in viewWillAppear ...

if (height > x) { // x would just be below the height you wanted to change it for 
    _verticalSpace1.constant = 20;
}

      

This changes the spacing I previously set in the storyboard to 8, to 20. Hopefully this solves your problem.

+1


source







All Articles