Reputation: 4065
I am trying to change a constraint for an ImageView for iPhone 4S,5,5S in viewDidLoad:
for (NSLayoutConstraint *constraint in logoImage.constraints) {
if ([constraint.identifier isEqualToString:@"logoTopIdentifier"]) {
constraint.constant=10;
}
}
It seems that it's not even iterating in the loop. Is there any other way to get a specific constraint with identifier?
Upvotes: 7
Views: 3239
Reputation: 1709
You can connect the constraint in storyboard to your view controller class same as connecting a UI element.
Just find the constraints in your storyboard, make the workspace into split view, and drag the constraint to your corresponding view controller class.
Sometimes if you want to animate the position change, you can update the constraint like:
self.theConstraint?.constant = 100
self.view.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.7) { () -> Void in
self.view.layoutIfNeeded()
}
block.
That is it.
Upvotes: 4
Reputation: 1852
Here is the KVConstraintExtensionsMaster
library by which you can access the any constant from a view
based on the NSLayoutAttribute
. No matter whether that constraint added Programmatically
or from Interface Builder
.
[self.logoImage accessAppliedConstraintByAttribute:NSLayoutAttributeTop completion:^(NSLayoutConstraint *expectedConstraint){
if (expectedConstraint) {
expectedConstraint.constant = 10;
/* for the animation */
[self.logoImage updateModifyConstraintsWithAnimation:NULL];
}
}];
Upvotes: 2