How to remove constraints programmatically that is added from storyboard?
As @Henit mentioned, you can set IBOutlet for constraints as well.
For example,
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
so now, you can remove this constraint like this:
[myView removeConstraint: viewHeight];
Or else if you want to remove all / multiple constraints related to your view then,
[myView removeConstraints: constraintsArrayHere]; // custom array of constraints references[myView removeConstraints: [myView constraints]]; //all constraints
Then later you can add your new constraints in the same manner using addConstraint
or addConstraints
method.
For more details go through Apple Documentation here.
Hope this helps.
removeConstraints will be deprecated in future.
You can use the following as alternative
viewHeight.active = NO;
To expand on @Megamind's answer: you can use the active
property of NSLayoutConstraint
. Just setup two different constraints for the two cases and activate only one of them depending on the login status. In InterfaceBuilder the active
property is oddly called Installed
:
Then in your code switch between the two:
- (void)setupRegistrationView{ _loadingIndicatorTopConstraintLogin.active = NO; _loadingIndicatorTopConstraintRegister.active = YES;}- (void)setupLoginView{ _loadingIndicatorTopConstraintLogin.active = YES; _loadingIndicatorTopConstraintRegister.active = NO;}
BTW, using the new UIStackView may provide a more elegant solution for your case but that's another topic.