0

screenshot of constraints

I want to learn if there's a way to delete AND create a constraint

So I have greenTop constrained to viewTop, purpleTop to greenBottom etc...

I want to be able to delete greenTop-viewTop constraint, delete purpleTop-greenBottom...and create a new purpleTop-viewTop constraint and a greenTop-blackBottom. (programmatically)

Effectively moving green to bottom and all others shift up one space.

Note: I know i could just make each constrained to topView and adjust, but I'd like to learn this way.

2
  • are you sure you don't want to use a UITableView? Commented Apr 16, 2015 at 20:16
  • this was an exercise for myself to learn deleting and addition of constraints Commented Apr 16, 2015 at 20:17

1 Answer 1

1

Sure, you can do that. You can make an IBOutlet to a constraint just like any other object. Call removeConstraint on the owner of that constraint to remove it,

someView.removeConstraint(someConstraint) // someConstraint is an IBOutlet

You cannot change the items or a relationship in a constraint, so #2 will not work.

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

3 Comments

@Dustin, what do you mean? How to create a constraint in code? Look at the methods in the NSLayoutConstraint class reference.
ok so I could create all possible constraints and activate/deactivate as need. But I can't understand the constraintsWithVisualFormat in reference, so I guess i could also use the init(item:attribute:relatedBy:toItem:attribute:multiplier:constant:) reference
@Dustin, if you don't understand it, read the document, "Auto Layout Concepts", and watch the videos on auto layout from the 2012 WWDC.

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.