Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - changing constraint relation programmatically

given the following constraint in ios programmatically:

IBOutlet NSLayoutConstraint *myConstraint;

this constraint is linked in interfacebuilder to the following details: enter image description here

How do I change the relation attribute programmatically. I tried to look up for a method called setRelation but I don't see it.

like image 310
j2emanue Avatar asked May 21 '15 18:05

j2emanue


2 Answers

According to the documentation, relation is read-only.

What you will need to do, I suspect, is to set

self.myConstraint.active = NO;

Then make a new NSLayoutConstraint programmatically using:

+ constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:

And in the process copying values you want to keep, and replacing the relation.

Then add it to the view hierarchy where appropriate.

like image 82
i_am_jorf Avatar answered Nov 05 '22 12:11

i_am_jorf


You can do like that :

  [self.view addConstraint:[NSLayoutConstraint
                                 constraintWithItem:self.yellowView
                                 attribute:NSLayoutAttributeWidth
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:self.redView
                                 attribute:NSLayoutAttributeWidth
                                 multiplier:0.75
                                 constant:0.0]];
like image 35
Erhan Demirci Avatar answered Nov 05 '22 11:11

Erhan Demirci