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?
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.
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];
}
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With