Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing constraint not applying when programmatically adding UIConstraints

I'm trying to add a subview to a view with constraints through code. I had some success but the trailing constraint seems to be completely ignored for whatever reason.

My code:

leading_const = 16.f;
trailing_const = 16.f;
top_const = 12.f;
bottom_const = 12.f;


insertView.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:insertView];
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:insertView
                                                           attribute:NSLayoutAttributeLeading
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:view
                                                           attribute:NSLayoutAttributeLeading
                                                          multiplier:1.f
                                                            constant:leading_const];
NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:insertView
                                                            attribute:NSLayoutAttributeTrailing
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:view
                                                            attribute:NSLayoutAttributeTrailing
                                                           multiplier:1.f
                                                             constant:trailing_const];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:insertView
                                                       attribute:NSLayoutAttributeTop
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:view
                                                       attribute:NSLayoutAttributeTop
                                                      multiplier:1.f
                                                        constant:top_const];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:insertView
                                                                   attribute:NSLayoutAttributeHeight
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:nil
                                                                   attribute:NSLayoutAttributeHeight
                                                                  multiplier:1.f
                                                                    constant:130.f];
[superView addConstraints:@[leading, trailing, top, height]];

Result:

enter image description here

Appreciate any guidance!

like image 414
Kevin Avatar asked Dec 15 '22 00:12

Kevin


1 Answers

Your constraint is being applied but as you have set it to 16 it is going 16 points past the trailing edge of the view. You therefore should use a negative value for the constant instead.

like image 85
Hodson Avatar answered Mar 15 '23 22:03

Hodson