ios 如何以编程方式删除从故事板添加的约束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27720842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove constraints programmatically that is added from storyboard?
提问by Dharmik
I have googled but not find out answer. So I need to ask.
I have one home screen. When User is logged in it will display one view as like bellow
Now When User logged out and visiting home page he will see above layout but without center boxed layout. If I set That layout hidden it is now displaying as follows.
我用谷歌搜索但没有找到答案。所以我需要问一下。我有一个主屏幕。当用户登录时,它将显示如下所示的一个视图。
当用户注销并访问主页时,他将看到上面的布局,但没有中心框的布局。如果我将那个布局设置为隐藏,它现在显示如下。
I want to move third layout to little bit above to remove white space..
我想将第三个布局移动到上方一点以删除空白..
I added constraints using storyboard. Now need to remove constraints from programming and add one constraints that will set layout to bellow first layout..
我使用故事板添加了约束。现在需要从编程中删除约束并添加一个约束,将布局设置为波纹管第一个布局..
回答by Mrunal
As @Henit mentioned, you can set IBOutlet for constraints as well.
正如@Henit 提到的,您也可以为约束设置 IBOutlet。
For example,
例如,
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
so now, you can remove this constraint like this:
所以现在,你可以像这样删除这个约束:
[myView removeConstraint: viewHeight];
Or else if you want to remove all / multiple constraints related to your view then,
或者,如果您想删除与您的视图相关的所有/多个约束,那么,
[myView removeConstraints: constraintsArrayHere]; // custom array of constraints references
[myView removeConstraints: [myView constraints]]; //all constraints
Then later you can add your new constraints in the same manner using addConstraint
or addConstraints
method.
然后,您可以使用addConstraint
或addConstraints
方法以相同的方式添加新约束。
For more details go through Apple Documentation here.
Hope this helps.
希望这可以帮助。
回答by Thein
removeConstraintswill be deprecated in future.
removeConstraints将来会被弃用。
You can use the following as alternative
您可以使用以下作为替代
viewHeight.active = NO;
回答by AXE
To expand on @Megamind's answer: you can use the active
property of NSLayoutConstraint
. Just setup two different constraints for the two cases and activate only one of them depending on the login status.
In InterfaceBuilder the active
property is oddly called Installed
:
为了扩大对@超级大坏蛋的回答是:你可以使用active
的特性NSLayoutConstraint
。只需为这两种情况设置两种不同的约束,并根据登录状态仅激活其中一种。在 InterfaceBuilder 中,该active
属性被奇怪地称为Installed
:
Then in your code switch between the two:
然后在您的代码中在两者之间切换:
- (void)setupRegistrationView
{
_loadingIndicatorTopConstraintLogin.active = NO;
_loadingIndicatorTopConstraintRegister.active = YES;
}
- (void)setupLoginView
{
_loadingIndicatorTopConstraintLogin.active = YES;
_loadingIndicatorTopConstraintRegister.active = NO;
}
BTW, using the new UIStackView may provide a more elegant solution for your case but that's another topic.
顺便说一句,使用新的 UIStackView 可能会为您的案例提供更优雅的解决方案,但这是另一个主题。
回答by Ariven Nadar
In Swift 4
在斯威夫特 4
@IBOutlet weak var viewHeight: NSLayoutConstraint!
viewHeight.isActive = false
Happy Coding :)
快乐编码:)
回答by Henit Nathwani
Take the IBOutlet of the height constraint of view you want to hide when the user logs out.
取用户注销时要隐藏的视图高度约束的IBOutlet。
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
There is one property constantin NSLayoutConstraintclass. You need to set it when the user logs in / logs out.
有一个属性不变的NSLayoutConstraint类。您需要在用户登录/注销时进行设置。
viewHeight.constant = isLoggedIn ? 30.0 : 0.0;
Hope this helps..
希望这可以帮助..
回答by blackjacx
It is pretty simple from iOS 10+ you can simply iterate over all constraints of a view and deactivate it. If you, e.g. want to find and remove the height constraint of a view you can do the following:
从 iOS 10+ 开始,这非常简单,您可以简单地遍历视图的所有约束并将其停用。如果您,例如,想要查找并删除视图的高度约束,您可以执行以下操作:
for constraint in constraints {
guard constraint.firstAnchor == heightAnchor else { continue }
constraint.isActive = false
break
}
ALTERNATIVE
It is even a one-liner. If you are inside your view you can just write:
替代方案
它甚至是一个单线。如果你在你的视图中,你可以写:
constraints.first { self.verticalSpacingFromThirdToFirstConstraint.constant = isLoggedIn ? 30.0 : 0.0
.firstAnchor == heightAnchor }?.isActive = false
回答by Keerthi Polepalle
You can do it in an other way too. Add a vertical spacing constraint between third layout and first layout which will be 30. Then add a reference to the constraint in the controller.
你也可以用其他方式来做。在第三个布局和第一个布局之间添加一个垂直间距约束,即 30。然后在控制器中添加对约束的引用。
_heightconstrainOutlet.constant=0;
PS: You should not add a height constraint for the middle view in this case. Just add the four trailing, leading, top(to first layout) and bottom (to third layout).
PS:在这种情况下,您不应为中间视图添加高度约束。只需添加四个尾随、前导、顶部(到第一个布局)和底部(到第三个布局)。
回答by Thanveer Ahammed
You can add a height constraint for that view you want to hide.
And add an NSlayoutHeightcontraint
outlet for that height constraint in your viewcontroller.h file. Then you can call that heightConstrain
outlet in your viewcontroller.m file wherever you need. Add this code where you want to hide this UIview
:
您可以为要隐藏的视图添加高度约束。并NSlayoutHeightcontraint
在 viewcontroller.h 文件中为该高度约束添加一个出口。然后您可以heightConstrain
在您需要的任何地方在 viewcontroller.m 文件中调用该插座。在要隐藏此代码的位置添加此代码UIview
:
constraints.filter({ let topConstraint = view.constraints.first(where: { ##代码##.firstAttribute == .top })
topConstraint?.constant = 20
.firstAnchor == heightAnchor }).forEach{ ##代码##.isActive = false }
It will make that height become zero. So your bottom view will cover that area too. If your bottom view had a height constraint and bottom space to container constraint? Just remove any one of them as your requirement .Thank you
它会使高度变为零。因此,您的底部视图也将覆盖该区域。如果您的底部视图具有高度约束和容器底部空间约束?只需根据您的要求删除其中任何一个即可。谢谢
回答by Tharak
I had a similar problem with a tableView that had another tableView in a cell, the constraints for height were stacking up as the cell got reused. To solve this i did:
我在一个单元格中有另一个 tableView 的 tableView 遇到了类似的问题,当单元格被重用时,高度的约束正在堆积。为了解决这个问题,我做了:
##代码##回答by Trevor
If you don't have access to the constraint from the interface builder you could update a constraint with:
如果您无权从界面构建器访问约束,则可以使用以下命令更新约束:
##代码##Where 'view' is whichever view you are attempting to update its constraints. There are some downsides to this approach in that you may have more than one top constraint associated with the view in question.
其中“视图”是您尝试更新其约束的任何视图。这种方法有一些缺点,因为您可能有多个与相关视图相关的顶部约束。