Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 - Find constraints for a UIView with identifier

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?

like image 423
BlackM Avatar asked Nov 28 '15 12:11

BlackM


2 Answers

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.

like image 134
Surely Avatar answered Nov 13 '22 09:11

Surely


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];
      }
    }];
like image 43
keshav vishwkarma Avatar answered Nov 13 '22 10:11

keshav vishwkarma