Search code examples
iosxcodeautolayoutnslayoutconstraint

Error when creating constraints programmatically


I have 2 views: aView and topView. Both of these have all the necessary constraints made in the interface builder.

Now I need to add a WKWebView instance to the view. So I've created a webPage property. Then I try to init the thing and add 4 constraints to it like so:

self.webPage = [[WKWebView alloc] init];

NSLayoutConstraint *topConstraint =[NSLayoutConstraint
                                   constraintWithItem: self.webPage
                                   attribute:NSLayoutAttributeTop
                                   relatedBy:NSLayoutRelationEqual
                                   toItem: self.topBar
                                   attribute:NSLayoutAttributeBottom
                                   multiplier:1.0
                                   constant:0.0];


NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
                                    constraintWithItem: self.webPage
                                    attribute:NSLayoutAttributeBottom
                                    relatedBy:NSLayoutRelationEqual
                                    toItem: self.aView
                                    attribute:NSLayoutAttributeBottom
                                    multiplier:1.0
                                    constant:50.0];


NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
                                    constraintWithItem: self.webPage
                                    attribute:NSLayoutAttributeLeading
                                    relatedBy:NSLayoutRelationEqual
                                    toItem: self.aView
                                    attribute:NSLayoutAttributeLeading
                                    multiplier:1.0
                                    constant:0.0];


NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
                                    constraintWithItem: self.webPage
                                    attribute:NSLayoutAttributeTrailing
                                    relatedBy:NSLayoutRelationEqual
                                    toItem: self.aView
                                    attribute:NSLayoutAttributeTrailing
                                    multiplier:1.0
                                    constant:0.0];

[self.aView addSubview: self.webPage];

[self.aView addConstraints: @[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint]];

so that my WKWebView instance is always right under topView, 50 pt above aView.bottom and is stuck to the edges of the view.

But I get errors:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 

and I can't understand what I did wrong.


Solution

  • You need to set translatesAutoresizingMaskIntoConstraints to false

    self.webPage.translatesAutoresizingMaskIntoConstraints = false