Prevent Interface Builder from auto creating Constraints?
Asked Answered
B

2

8

I created a demo project here. I have a view where I created a ScrollView in a .xib file. In interface builder I did not set any constraints. In my viewDidLoad method I set constraints with SnapKit:

scrollView.snp_makeConstraints { (make) -> Void in
  make.edges.equalTo(self.view)
}

When I run the code I get the following console output:

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x7b74e280 'IB auto generated at build time for view with fixed frame' V:|-(1)-[UIScrollView:0x7b74c430]   (Names: '|':UIView:0x7b74dd10 )>",
    "<<DeviceImagesTest.LayoutConstraint:0x7baa2760> <UIScrollView:0x7b6608d0>.top == <UIView:0x7b64d800>.top>"
)

Will attempt to recover by breaking constraint 
<NSIBPrototypingLayoutConstraint:0x7b74e280 'IB auto generated at build time for view with fixed frame' V:|-(1)-[UIScrollView:0x7b74c430]   (Names: '|':UIView:0x7b74dd10 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-29 18:37:14.368 DeviceImagesTest[36462:607] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x7b74e080 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIScrollView:0x7b74c430](LTR)   (Names: '|':UIView:0x7b74dd10 )>",
    "<NSIBPrototypingLayoutConstraint:0x7b74e330 'IB auto generated at build time for view with fixed frame' H:[UIScrollView:0x7b74c430(600)]>",
    "<<DeviceImagesTest.LayoutConstraint:0x7baa7170> <UIScrollView:0x7baa7b30>.right == <UIView:0x7baa79a0>.right>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa4800 h=-&- v=-&- UIView:0x7b74dd10.width == UIViewControllerWrapperView:0x7ba9f060.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa5180 h=-&- v=-&- UIViewControllerWrapperView:0x7ba9f060.width == UINavigationTransitionView:0x7bc769b0.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa58e0 h=-&- v=-&- UINavigationTransitionView:0x7bc769b0.width == UILayoutContainerView:0x7bc75140.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa61a0 h=-&- v=-&- UILayoutContainerView:0x7bc75140.width == UIWindow:0x7bc71870.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa69a0 h=--- v=--- H:[UIWindow:0x7bc71870(320)]>"
)

Will attempt to recover by breaking constraint 
<<DeviceImagesTest.LayoutConstraint:0x7baa95b0> <UIScrollView:0x7baa9610>.right == <UIView:0x7baa96b0>.right>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-29 18:37:14.496 DeviceImagesTest[36462:607] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSIBPrototypingLayoutConstraint:0x7b74e360 'IB auto generated at build time for view with fixed frame' V:[UIScrollView:0x7b74c430(600)]>",
    "<<DeviceImagesTest.LayoutConstraint:0x7bc77e40> <UIScrollView:0x7bc7f6a0>.top == <UIView:0x7bc52180>.top>",
    "<<DeviceImagesTest.LayoutConstraint:0x7bc49420> <UIScrollView:0x7bc74b00>.bottom == <UIView:0x7bc7b970>.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa4960 h=-&- v=-&- UIView:0x7b74dd10.height == UIViewControllerWrapperView:0x7ba9f060.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa51e0 h=-&- v=-&- UIViewControllerWrapperView:0x7ba9f060.height == UINavigationTransitionView:0x7bc769b0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa5940 h=-&- v=-&- UINavigationTransitionView:0x7bc769b0.height == UILayoutContainerView:0x7bc75140.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa6200 h=-&- v=-&- UILayoutContainerView:0x7bc75140.height == UIWindow:0x7bc71870.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x7baa69d0 h=--- v=--- V:[UIWindow:0x7bc71870(568)]>"
)

Will attempt to recover by breaking constraint 
<<DeviceImagesTest.LayoutConstraint:0x7bc84790> <UIScrollView:0x7bc848b0>.bottom == <UIView:0x7bc84860>.bottom>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

It seems that Interface Builder is generating Constraints for me even if I did not set any. This leads to a conflict with my constraints I set in code. I also set setTranslatesAutoresizingMasks(false).

How can I prevent Interface Builder from auto generating constraints?

Bologna answered 29/5, 2015 at 16:41 Comment(2)
If you are setting no constraints in the storyboard, then in code, scrollview.setTranslatesAutoresizingMasksIntoConstraints(false)Summand
I use interface builder not Storyboard and I did setTranslatesAutoresizingMasks(false)Bologna
P
13

If you are confused about the NSAutoresizingMaskLayoutConstraints in that list, it might be because you haven't called setTranslatesAutoresizingMasksIntoContraints(false) on that view. (although, I was under the impression that SnapKit does that for you).

If you are confused about the NSIBPrototypingLayoutConstraint, that's an auto-generated constraint by Interface Builder. To get rid of it, add some Top, Bottom, Leading and Trailing constraints in Interface Builder and set them to be removed at build time. For more details about that, have a look at http://travisjeffery.com/b/2013/11/preventing-ib-auto-generated-at-build-time-for-view-with-fixed-frame/

UPDATE: url was changed to http://travisjeffery.com/b/2013/11/preventing-ib-auto-generated-at-build-time-for-view-with-fixed-frame-when-using-auto-layout/

Potvaliant answered 3/6, 2015 at 12:22 Comment(0)
T
0

How can I prevent Interface Builder from auto generating constraints?

Simplest way: select the nib or storyboard in the project navigator, go to the File inspector, and uncheck "Use Auto Layout".

Testify answered 29/5, 2015 at 16:52 Comment(7)
Go right ahead. We are simply turning of autolayout behavior in the nib, which is exactly what you want to do. You can still use it in code. Try it!Testify
Does not work that way. You can check my example I still get the warnings.Bologna
No, you're talking about these: NSAutoresizingMaskLayoutConstraint. Those are because you forgot to setTranslatesAutoresizingMasksIntoContraints(false). I'm talking about these: NSIBPrototypingLayoutConstraint. That is what you asked about. Those are prevented if you turn off auto layout in the nib.Testify
I still do not get it. What is wrong in my example here: github.com/confile/SnapKit-TestBologna
You are using a third-party library, SnapKit. I don't do that stuff. That's just another layer of confusion between you and reality. I assure you that what I'm saying is true; I do this all the time.Testify
still I do not get what to do!Bologna
Your question was: "How can I prevent Interface Builder from auto generating constraints?". I told you the answer to that. If you have a different question, that's a different question.Testify

© 2022 - 2024 — McMap. All rights reserved.