15

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.

1
  • 1
    What are you trying to do by changing the relation? There might be an easier way. Commented May 21, 2015 at 18:35

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.