How to remove constraints programmatically that is added from storyboard?

IosXcode StoryboardIos Autolayout

Ios Problem Overview


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..

Ios Solutions


Solution 1 - Ios

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 - Ios

removeConstraints will be deprecated in future.

You can use the following as alternative

viewHeight.active = NO;

Solution 3 - Ios

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 - Ios

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 - Ios

In Swift 4

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

Happy Coding :)

Solution 6 - Ios

Take the IBOutlet of the height constraint of view you want to hide when the user logs out.

@property(weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;

There is one property constant in NSLayoutConstraint class. You need to set it when the user logs in / logs out.

viewHeight.constant = isLoggedIn ? 30.0 : 0.0;

Hope this helps..

Solution 7 - Ios

You can do it in an other way too. Add a vertical spacing constraint between third layout and first layout which will be 30. Then add a reference to the constraint in the controller.

self.verticalSpacingFromThirdToFirstConstraint.constant = isLoggedIn ? 30.0 : 0.0

PS: You should not add a height constraint for the middle view in this case. Just add the four trailing, leading, top(to first layout) and bottom (to third layout).

Solution 8 - Ios

You can add a height constraint for that view you want to hide. And add an NSlayoutHeightcontraint outlet for that height constraint in your viewcontroller.h file. Then you can call that heightConstrain outlet in your viewcontroller.m file wherever you need. Add this code where you want to hide this UIview:

_heightconstrainOutlet.constant=0;

It will make that height become zero. So your bottom view will cover that area too. If your bottom view had a height constraint and bottom space to container constraint? Just remove any one of them as your requirement .Thank you

Solution 9 - Ios

I had a similar problem with a tableView that had another tableView in a cell, the constraints for height were stacking up as the cell got reused. To solve this i did:

constraints.filter({$0.firstAnchor == heightAnchor }).forEach{ $0.isActive = false }

Solution 10 - Ios

If you don't have access to the constraint from the interface builder you could update a constraint with:

    let topConstraint = view.constraints.first(where: { $0.firstAttribute == .top })
    topConstraint?.constant = 20

Where 'view' is whichever view you are attempting to update its constraints. There are some downsides to this approach in that you may have more than one top constraint associated with the view in question.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDharmikView Question on Stackoverflow
Solution 1 - IosMrunalView Answer on Stackoverflow
Solution 2 - IosTheinView Answer on Stackoverflow
Solution 3 - IosAXEView Answer on Stackoverflow
Solution 4 - IosblackjacxView Answer on Stackoverflow
Solution 5 - IosAriven NadarView Answer on Stackoverflow
Solution 6 - IosHenit NathwaniView Answer on Stackoverflow
Solution 7 - IosKeerthi PolepalleView Answer on Stackoverflow
Solution 8 - IosThanveer AhammedView Answer on Stackoverflow
Solution 9 - IosTharakView Answer on Stackoverflow
Solution 10 - IosTrevorView Answer on Stackoverflow