11

I know how to modify a existing constraint. But I would to know if someone has found a solution to get a constraint without save this one as a property.

Current solution to set Constraint height:

1) save NSLayoutConstraint in a variable:

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:myView
                                                                    attribute:NSLayoutAttributeHeight
                                                                    relatedBy:NSLayoutRelationEqual
                                                                       toItem:nil
                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                   multiplier:1.0f
                                                                     constant:20];
[self.view addConstraint:heightConstraint];

2) Set the constant of the Constraint saved to "0.0" ( to hide this view)

[heightConstraint setConstant:200];

I'm looking for a solution like this:

 [myView setConstraint:@"0." forAttribute:NSLayoutAttributeHeight]
Christoph
  • 1,965
  • 16
  • 35
Damien Romito
  • 9,801
  • 13
  • 66
  • 84

2 Answers2

14

I just built this Category (https://github.com/damienromito/UIView-UpdateAutoLayoutConstraints) that update constrains when you want:

//you can use tools to hide/show a uiview
[myView1 hideByHeight:YES];

enter image description here

Or just do it to hide an UIView with autolayout:

//Hide View 
[myView1 setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];
Damien Romito
  • 9,801
  • 13
  • 66
  • 84
6

UIView has a method that returns all constraints affecting its layout in one of the dimensions:

NSArray *constraints = 
[someView constraintsAffectingLayoutForAxis:UILayoutConstraintAxisVertical];

Then you can just find the one you're interested in.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • this method return an empty NSArray. To get all constraints "NSArray *constraints = [self.view constraints]" works very good. – Damien Romito Mar 13 '14 at 16:39
  • I built a category, look at the EDIT of my Question. – Damien Romito Mar 13 '14 at 17:35
  • @DamienRomito Great. Consider posting it as a separate answer. http://stackoverflow.com/help/self-answer – DrummerB Mar 13 '14 at 17:49
  • The documentation for `constraintsAffectingLayoutForAxis:` specifically states "This method should only be used for debugging constraint-based layout. No application should ship with calls to this method as part of its operation." – Benjohn Jan 29 '15 at 15:02
  • 1
    @DamienRomito `constraints` does not provide you with all constraints that effect a view. It only provides you with the constraints that a particular view hosts. They may not effect it at all (only its children). – Benjohn Jan 29 '15 at 15:04
  • 1
    This method should only be used for debugging constraint-based layout. No application should ship with calls to this method as part of its operation. – Wingzero Apr 01 '15 at 07:41