Dam*_*ito 11 objective-c show-hide uiview autolayout nslayoutconstraint
我知道如何修改现有约束.但我想知道是否有人找到了一个解决方案来获得一个约束而不保存这个属性.
当前设置约束高度的解决方案:
1)将NSLayoutConstraint保存在变量中:
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:myView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0f
constant:20];
[self.view addConstraint:heightConstraint];
Run Code Online (Sandbox Code Playgroud)
2)将约束的常量设置为"0.0"(隐藏此视图)
[heightConstraint setConstant:200];
Run Code Online (Sandbox Code Playgroud)
我正在寻找这样的解决方案:
[myView setConstraint:@"0." forAttribute:NSLayoutAttributeHeight]
Run Code Online (Sandbox Code Playgroud)
Dam*_*ito 14
我刚建立了这个类别(https://github.com/damienromito/UIView-UpdateAutoLayoutConstraints),可以根据需要更新约束:
//you can use tools to hide/show a uiview
[myView1 hideByHeight:YES];
Run Code Online (Sandbox Code Playgroud)
或者只是用autolayout隐藏UIView:
//Hide View
[myView1 setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];
Run Code Online (Sandbox Code Playgroud)
UIView有一个方法可以在其中一个维度中返回影响其布局的所有约束:
NSArray *constraints =
[someView constraintsAffectingLayoutForAxis:UILayoutConstraintAxisVertical];
Run Code Online (Sandbox Code Playgroud)
然后你就可以找到你感兴趣的那个.