How to remove constraints programmatically that is added from storyboard?

I have googled but not find out answer. So I need to ask. I have one home screen. When User is logged in it will display one view as like bellow enter image description here Now When User logged out and visiting home page he will see above layout but without center boxed layout. If I set That layout hidden it is now displaying as follows. enter image description here

I want to move third layout to little bit above to remove white space..

I added constraints using storyboard. Now need to remove constraints from programming and add one constraints that will set layout to bellow first layout..


Solution 1:

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.

Solution 2:

removeConstraints will be deprecated in future.

You can use the following as alternative

viewHeight.active = NO;

Solution 3:

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:

Login constraintRegister constraint

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.

Solution 4:

It is pretty simple from iOS 10+ you can simply iterate over all constraints of a view and deactivate it. If you, e.g. want to find and remove the height constraint of a view you can do the following:

for constraint in constraints {
    guard constraint.firstAnchor == heightAnchor else { continue }
    constraint.isActive = false
    break
}

ALTERNATIVE

It is even a one-liner. If you are inside your view you can just write:

constraints.first { $0.firstAnchor == heightAnchor }?.isActive = false

2nd ALTERNATIVE

Don't use storyboards - they are evil and in future (with SwiftUI) they will be outdated. The only truth lies in code 💪

Solution 5:

In Swift 4

@IBOutlet weak var viewHeight: NSLayoutConstraint!
viewHeight.isActive = false    

Happy Coding :)