Why to appear a constraint called NSIBPrototypingLayoutConstraint

In general, the constraint type is NSLayoutConstraint, but since Xcode 4, when we added Auto Layout, we were concerned that when a developer created a View in IB, they would not be able to display it without adding any constraints. So the IB will, according to the position that you drag the View automatically help you add constraint, avoid the View cannot show, because of this constraint is the IB automatically add, so class named NSIBPrototypingLayoutConstraint, And this constraint does not pass NSLog(@”%@”, self.constraints); Print it out.

Deep reasons

In general, in this case, set the current View

view.translatesAutoresizingMaskIntoConstraints = NO;
Copy the code

Can.

For translatesAutoresizingMaskIntoConstraints attribute, the official document:

Discussion If this property’s value is YES, The system creates a set of constraints that duplicate the behavior specified by the view’s autoresizing mask. This also creates a set of constraints that duplicate the behavior specified by the view’s autoresizing mask Lets you modify the view’s size and location using the view’s frame, bounds, or center properties, allowing you to create a static, frame-based layout within Auto Layout.

Note that the autoresizing mask constraints fully specify the view’s size and position; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts. If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to NO, and then provide a nonambiguous, nonconflicting set of constraints for the view.

By default, the property is set to YES for any view you programmatically create. If you add views in Interface Builder, the system automatically sets this property to NO.

When you create a View in code, this property defaults to YES, and you automatically convert it to an autoLayout constraint by setting its frame, bounds, center, etc. Ensure the integrity of view constraints and good display.

However, once you do that, you can’t add additional constraints to the View. Because the View already has a transfer constraint, if you add it, the constraint will conflict.

If you want to be able to manually add the constraints, to meet the demand, you will need to set the translatesAutoresizingMaskIntoConstraints = NO.

When using IB, this property defaults to NO.

A bit confusing. Officially, in IB, this property should be NO, but the UITableViewCell I created through the xib, in the awakeFromNib method, prints this property as YES. This leads to a conflict with the constraint I’ll add in the awakeFromNib method.

The ultimate solution to the problem

In IB, add some constraints to the view and select all of them. In the File Inspector, check Remove at Build Time;

 

Why does the above approach solve the problem

The root of the problem is that IB automatically creates constraints that we don’t want. IB automatically creates constraints because there are no constraints on the view, so we randomly add unnecessary constraints to the view and have it removed during building. In this way, when the code executes to the navigation Make to create the constraint, the view will be clean.

Placeholder: Remove At build time

This property is actually used in this way: sometimes during development, when we need to preview some layout, it’s convenient to add constraints to our Storyboard so that we can test and verify that our constraints are correct without executing the code.

In real life, however, you might find it difficult to add constraints to a storyboard, and you might want to use code to create constraints, such as a tool like Navigation.

It’s as simple as removing all the constraints that have been added to the storyboard at run time. This way, the constraints added to the storyboard don’t have any effect on the constraints added to the code. On the other hand, layout in storyboard is much easier, and you don’t get a bunch of constraints warnings in storyboard.

Afterword.

If you have any questions, please leave a message and I will get back to you as soon as possible