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

◇◆丶佛笑我妖孽 提交于 2019-11-26 11:27:42

问题


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]

回答1:


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

Or just do it to hide an UIView with autolayout:

//Hide View 
[myView1 setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];



回答2:


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.



来源:https://stackoverflow.com/questions/22384927/hide-autolayout-uiview-how-to-get-existing-nslayoutconstraint-to-update-this-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!