ios 删除视图时约束会发生什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18617991/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:59:35  来源:igfitidea点击:

What happens with constraints when a view is removed

iosautolayoutnslayoutconstraint

提问by Sulthan

The question I have is simple but I couldn't find any information in the documentation.

我的问题很简单,但我在文档中找不到任何信息。

What happens with layout constraints when a view is removed from the view hierarchy (or moved to another view)?

当视图从视图层次结构中移除(或移动到另一个视图)时,布局约束会发生什么?

For example, let's have container Cwith subviews Aand B. Container Cholds some constraints. Then we call [A removeFromSuperview]. What happens with the constraints for A?

例如,让我们有一个包含C子视图AB. 容器C有一些限制。然后我们调用[A removeFromSuperview]. 的约束会发生什么A

What then happens if we add Ato Cagain?

那么如果我们增加会发生什么AC着?

回答by rdelmar

The constraints are removed. If you add A again, you will have to make new constraints for it, or if you save the constraints before you remove A, you can add them back. When I do something like this, I save the constraints like this for a view called view1:

约束被移除。如果再次添加 A,则必须为其创建新约束,或者如果在删除 A 之前保存约束,则可以重新添加它们。当我做这样的事情时,我会为一个名为 view1 的视图保存这样的约束:

self.portraitConstraints = [NSMutableArray new];
for (NSLayoutConstraint *con in self.view.constraints) {
    if (con.firstItem == self.view1 || con.secondItem == self.view1) {
       [self.portraitConstraints addObject:con];
    }
}

回答by Evan Stone

Since I had this question too, I checked the Apple Docs just for kicks, and it turns out that it is documented that the constraints are removed.

因为我也有这个问题,所以我检查了 Apple Docs 只是为了好玩,结果证明限制被删除了。

The documentationfor the UIView removeFromSuperview method states:

UIView removeFromSuperview 方法的文档说明:

Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing.

调用此方法将删除引用您要删除的视图或引用您要删除的视图的子树中的任何视图的任何约束。

I'm not sure if this was documented last year when the original question was posted, but I just thought I'd share this information in case anyone needed it...

我不确定去年发布原始问题时是否记录了这一点,但我只是想我会分享这些信息,以防有人需要它......

回答by Martin Redington

Be aware though, that if you have two independent parent views A and B, and a subview C, where C is currently a subview of A, with appropriate constraints, that calling [B addSubview:C] will NOT clear any constraints relating to A and C, and auto layout will start throwing exceptions, because those constraints no longer relate to views in the same hierarchy.

但是请注意,如果您有两个独立的父视图 A 和 B,以及一个子视图 C,其中 C 当前是 A 的子视图,并且具有适当的约束,那么调用 [B addSubview:C] 将不会清除与 A 相关的任何约束和 C 和自动布局将开始抛出异常,因为这些约束不再与同一层次结构中的视图相关。

You will need to call [C removeFromSuperview] explicitly to remove the constraints, before adding C to B.

在将 C 添加到 B 之前,您需要显式调用 [C removeFromSuperview] 以删除约束。

This is true on Mac OS X - I haven't checked iOS

这在 Mac OS X 上是正确的 - 我没有检查过 iOS

回答by Mike Pollard

The constraints are also removed when you [A removeFromSuperview]

当您 [A removeFromSuperview]

They are forgotten and adding A to C again adds no constraints.

它们被遗忘了,再次将 A 添加到 C 不会增加任何约束。

回答by Andrea

They are removed too, you can do a simple test. Pick up a view SUBVIEW and create costraints that constraint SUBVIEW to follow its superview resizing (like attched to to superview edges). To do that you add SUBVIEW as a subview to this CONTAINERVIEW and add as constraints something like that:
V:|-[SUBVIEW]-|
H:|-[SUBVIEW]-|
These constraints should be added to SUBVIEW superview, thus CONTAINERVIEW.
If you remove SUBVIEW by simply checking all the CONTAINERVIEW constraints you could see that two aren't around anymore.

它们也被删除了,你可以做一个简单的测试。选择一个视图 SUBVIEW 并创建约束 SUBVIEW 跟随其父视图调整大小的 cotraints(如附加到超级视图边缘)。为此,您将 SUBVIEW 作为子视图添加到此 CONTAINERVIEW 并添加如下约束:
V:|-[SUBVIEW]-|
H:|-[订阅]-|
这些约束应该添加到 SUBVIEW 超视图,因此是 CONTAINERVIEW。
如果您通过简单地检查所有 CONTAINERVIEW 约束来删除 SUBVIEW,您会看到两个不再存在。

回答by William Hu

This question also can be proved by interface builder. When drag and drop a UIViewon the ViewControlleradd constraints then remove the UIView, you can see the blue constraints disappear.

这个问题也可以通过界面生成器来证明。当UIViewViewController添加约束上拖放 a然后删除UIView,您可以看到蓝色约束消失。