Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling/Disabling NSLayoutConstraints in InterfaceBuilder

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

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

Let me clear here I do NOT want to delete them, just disable them so that i can design a second set without interface builder complaining all the time about mismatching constraints. Furthermore switching size classes is not an option, since the layout sets are meant for one and the same size class.

Is there an option to do so?

Thanks in Advance

Malte

Further Information: SDK Version: 8.1 Deployment Target 8.0

like image 878
Malte Avatar asked Mar 13 '15 08:03

Malte


3 Answers

Select the constraint you want to disable in storyboard, and Option+Command+4 to show Attributes Inspector, then unselect Installed.

like image 92
gabbler Avatar answered Oct 27 '22 11:10

gabbler


I'd previously used the solution provided by Gabbler successfully, but recently I tried the same thing using Swift 2.0 and Xcode 7 and found that it no longer worked. Constraints that were set as not installed were, as one might expect, not installed at all and had no effect on the layout when switched on or off.

My solution to the problem was to ensure that all constraints were Installed and to add a user-defined runtime attribute with the key 'active', type 'boolean' and value 'false'.

The user-defined runtime attribute panel can be found in the Identity Inspector underneath the Custom Class fields.

like image 28
Ash Avatar answered Oct 27 '22 09:10

Ash


My solution using Xcode 8 and Swift 3 without getting any warnings was unchecked the Installed box on Interface builder, Inspector tab:

Installed box on Interface builder

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

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

Make sure to remove the constraints first, otherwise you will get the message log Unable to simultaneously satisfy constraints...

like image 35
rgkobashi Avatar answered Oct 27 '22 10:10

rgkobashi