Search code examples
iosobjective-cautolayoutconstraintsadaptive-layout

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?


Solution

  • 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.