Question

I'm creating a View in code (no xib or storyboard for this one) and setting the constraints in code, like:

NSDictionary *bindings = NSDictionaryOfVariableBindings(_myView);
_myView = [[UIView alloc] init];
[self.view addSubview:_myView];
_myView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_myView]-0-|" options:0 metrics:nil views:bindings ] ];
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_myView(25)]" options:0 metrics:nil views:bindings ] ];

Later in another method I want to change the position of _myView and animate this changing.

My problem: What is the best/correct way to change the set constraints? I don't know how to change e.g. the X and Y poisition. E.g. I set both values to 0 and I want to change X and Y to 50.

Was it helpful?

Solution

Constraints are like any other object, if you want to change them later make a property.

@property (nonatomic, strong) NSLayoutConstraint *centerConstraint;

Then set it to a particular constraint, add it to the view and you can remove and re-add it later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top