27

Is there a way to install and uninstall an Xcode interface builder constraint programmatically?

enter image description here

2
  • take a look at open source project like mansory and pure layout on github. They are better and easier to code Commented May 28, 2015 at 1:40
  • Is there a way to get this question unclosed, as the meaning of the question is now obvious, and the correct answer is in a comment of an outdated answer? Commented Oct 24, 2019 at 4:44

1 Answer 1

22

Connect the IBOutlet for the NSLayoutConstraint by CTRL+click the constraint and drag it to the viewController (for ex. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraint;).

For removing the constraint:

[self.view removeConstraint:constraint]

for installing the constraint:

[self.view addConstraint:constraint]
Sign up to request clarification or add additional context in comments.

15 Comments

So if a constraint starts out uninstalled, then by addConstraint will become installed?
@Eric "uninstall" is the wrong terminology here... uninstall is strictly in reference to the storyboard and has nothing to do with anything done programmatically at run time.
In iOS 8 there is an active property on constraint that corresponds to the "uninstalled" setting in XCode. Simply leave one constraint uninstalled in XCode with your alternate values and then switch between them by alternating their active states at run time.
Just for the record! It might be dangerous to have a weak reference to the constraint if it will be added/removed in code! It might get released!
This solution is deprecated. use NSLayoutConstraint's active property instead.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.