Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

objective c - Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...