Enabling / Disabling NSLayoutConstraints in the Builder Interface

NSLayoutConstraint (in iOS 8.0 ) has a BOOL property called active , which makes it easy to disable / enable the specified layout constraint on the fly.

To create a second set of layouts for the view controller, which I can then enable / disable programmatically (via IBOutletCollection of NSLayoutConstraints for both sets), I need to disable my already defined layout restrictions in the interface builder.

Let me clarify here, I DO NOT want to delete them, just turn them off so that I can design a second set without an interface constructor that complained all the time about inappropriate restrictions. In addition, switch class classes are not an option, since layout sets are for the same size class.

Is there any way to do this?

Thanks at Advance

Malta

Additional Information: SDK Version: 8.1 Deployment Target 8.0

+10
ios objective-c xcode autolayout interface-builder
source share
4 answers

Select the constraint that you want to disable in the storyboard, and Option + Command + 4 to display the attribute inspector, then clear the Installed check box.

+21
source share

I previously used the solution provided by Gabbler, but recently I tried the same with Swift 2.0 and Xcode 7 and found that it no longer works. Restrictions that were set as not set, as expected, were not set at all and did not affect the layout when turned on or off.

My solution to the problem was to make sure that all restrictions were set and add a user-defined runtime attribute with the key "active", enter "boolean" and the value "false".

The custom runtime attribute panel can be found in the Identity Inspector under the custom class fields.

+12
source share

My solution, using Xcode 8 and Swift 3 without receiving any warnings, was removed from the โ€œInstalledโ€ checkbox in the interface builder, โ€œInspectorโ€ tab:

Installed field in interface builder

Then create IBOutlets and add / remove them programmatically on viewDidLayoutSubviews ()

 view.removeConstraints([constraints to remove, ...]) view.addConstraints([constraints to add, ...]) 

Be sure to remove the restrictions first, otherwise you will receive a message log. It is impossible to simultaneously satisfy the restrictions ...

+2
source share

In this particular case, I declare my limitations in the code.

 // Card View Animatable Constraints private lazy var cardViewHeightConstraint: NSLayoutConstraint = cardView.heightAnchor.constraint(equalToConstant: 500) private lazy var cardViewEqualHeightConstraint: NSLayoutConstraint = cardView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.8) 

You can then activate or deactivate them whenever you want.

 override func viewDidLoad() { super.viewDidLoad() cardViewHeightConstraint.isActive = true cardViewEqualHeightConstraint.isActive = false } 

And you can still make the most of your layout in the interface builder. You can set a constraint that will be removed at run time so that Interface Builder still reflects the look of your layouts (or at least one of them). Just create a constraint and check the "Delete at build time" attribute.

In a constraint's size or attribute inspector, check the "Remove at build time" box.

What is achieved:

  • You should use Interface Builder for most of your layout.
  • You will not have any IB warnings
  • It works.

The only drawback is that the interface builder will not be able to fully represent your layout.

0
source share

All Articles