4

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!

Kevin
  • 43
  • 3

1 Answers1

12

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.

Hodson
  • 3,438
  • 1
  • 23
  • 51
  • This solved it! Interesting why it's a negative value in code but it would be a positive value in the interface builder? – Kevin Feb 06 '17 at 01:43
  • 3
    It depends on how you are relating the views with the constraint. In your case you was relating the trailing edge of `insertView` to the trailing edge of `view` so therefore you have to bring it 16 points back from the edge of `view`. If you was to relate your view the other way, `view` --> `insertView` then you would use a positive number to move it 16 points forward from the trailing edge of the `insertView`. I hope that makes sense. – Hodson Feb 06 '17 at 08:37
  • Thanks for the explanation! Makes total sense. – Kevin Feb 06 '17 at 10:12
  • @Hodson Thanks for the explanation. It solved my issue. – Ramakrishna Sep 05 '17 at 05:09