Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 976
Dharmik Avatar asked Dec 31 '14 12:12

Dharmik


People also ask

How do I remove a constraint from a storyboard?

To remove a single constraint just select it and hit delete. To remove constraints for a single view you select the view and hit the triangle button in the bottom right hand corner... And hit "Clear Constraints" under the "Selected Views" part.

How do you clear a constraint?

To delete a check constraint In Object Explorer, expand the table with the check constraint. Expand Constraints. Right-click the constraint and click Delete. In the Delete Object dialog box, click OK.

How do I remove a constraint in Swift?

When developing for iOS 8.0 or later, set the constraint's isActive property to false instead of calling the removeConstraint(_:) method directly. The isActive property automatically adds and removes the constraint from the correct view.


2 Answers

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.

like image 97
Mrunal Avatar answered Oct 21 '22 15:10

Mrunal


removeConstraints will be deprecated in future.

You can use the following as alternative

viewHeight.active = NO;
like image 30
Thein Avatar answered Oct 21 '22 14:10

Thein