'删除子视图'将删除相关约束.iOS如何工作?

时间:2016-10-25 03:44:07

标签: ios uiview nslayoutconstraint

假设containsView中有两个子视图。

enter image description here

不要考虑宽度和高度约束,只要认为它们等于常数。

Subview1:

对superview具有领先和最高限制。并且“水平约束”'到subview2。

Subview2:

有superview和top constaint到superview。

众所周知,如果我隐藏了subview2,那么水平约束'仍然存在。但是如果我从containsView中删除它。项目做什么来删除水平constaint'?

我认为subview2就像是muti-tree的根节点。如果它被removeFromSuperview,则根节点保留计数为0,然后子节点(如约束)retaincount释放... 另一个想法是当删除子视图时,uiview将遍历它的约束数组并删除一些与nil视图相关的约束。

我是对的吗?有人可以给我意见吗?

我试过这个,

enter image description here

这是删除和删除后面的日志:



<__NSArrayI 0x1740ec480>(
<_UILayoutSupportConstraint:0x17008a690 _UILayoutGuide:0x100211410.height == 20   (active)>,
<_UILayoutSupportConstraint:0x170089d80 V:|-(0)-[_UILayoutGuide:0x100211410]   (active, names: '|':UIView:0x100210b30 )>,
<_UILayoutSupportConstraint:0x17008a780 _UILayoutGuide:0x1002119c0.height == 0   (active)>,
<_UILayoutSupportConstraint:0x17008a730 _UILayoutGuide:0x1002119c0.bottom == UIView:0x100210b30.bottom   (active)>,
<NSLayoutConstraint:0x17008a500 UIView:0x100211270.leading == UIView:0x100210b30.leadingMargin + 6   (active)>,
<NSLayoutConstraint:0x17008a550 V:[_UILayoutGuide:0x100211410]-(210)-[UIView:0x100211270]   (active)>,
<NSLayoutConstraint:0x17008a5a0 H:[UIView:0x100211270]-(99)-[UIView:0x100210ed0]   (active)>,
<NSLayoutConstraint:0x17008a5f0 UIView:0x100210ed0.centerY == UIView:0x100211270.centerY   (active)>,
<NSLayoutConstraint:0x17008a640 UIView:0x100210b30.trailingMargin == UIView:0x100210ed0.trailing + 11   (active)>,
<NSLayoutConstraint:0x1740853c0 'UIView-Encapsulated-Layout-Height' UIView:0x100210b30.height == 667   (active)>,
<NSAutoresizingMaskLayoutConstraint:0x174085460 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' UIView:0x100210b30.minX == 0   (active, names: '|':UIWindow:0x100405340 )>,
<NSAutoresizingMaskLayoutConstraint:0x174085410 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' UIView:0x100210b30.minY == 0   (active, names: '|':UIWindow:0x100405340 )>,
<NSLayoutConstraint:0x1740852d0 'UIView-Encapsulated-Layout-Width' UIView:0x100210b30.width == 375   (active)>
)


(lldb) po self.view.constraints
<__NSArrayI 0x1700a7680>(
<_UILayoutSupportConstraint:0x17008a690 _UILayoutGuide:0x100211410.height == 20   (active)>,
<_UILayoutSupportConstraint:0x170089d80 V:|-(0)-[_UILayoutGuide:0x100211410]   (active, names: '|':UIView:0x100210b30 )>,
<_UILayoutSupportConstraint:0x17008a780 _UILayoutGuide:0x1002119c0.height == 0   (active)>,
<_UILayoutSupportConstraint:0x17008a730 _UILayoutGuide:0x1002119c0.bottom == UIView:0x100210b30.bottom   (active)>,
<NSLayoutConstraint:0x17008a500 UIView:0x100211270.leading == UIView:0x100210b30.leadingMargin + 6   (active)>,
<NSLayoutConstraint:0x17008a550 V:[_UILayoutGuide:0x100211410]-(210)-[UIView:0x100211270]   (active)>,
<NSLayoutConstraint:0x1740853c0 'UIView-Encapsulated-Layout-Height' UIView:0x100210b30.height == 667   (active)>,
<NSAutoresizingMaskLayoutConstraint:0x174085460 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' UIView:0x100210b30.minX == 0   (active, names: '|':UIWindow:0x100405340 )>,
<NSAutoresizingMaskLayoutConstraint:0x174085410 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' UIView:0x100210b30.minY == 0   (active, names: '|':UIWindow:0x100405340 )>,
<NSLayoutConstraint:0x1740852d0 'UIView-Encapsulated-Layout-Width' UIView:0x100210b30.width == 375   (active)>
)
&#13;
&#13;
&#13;

删除subview2后删除了这一行:

  

NSLayoutConstraint:0x17008a5a0 H:[UIView:0x100211270] - (99) - [UIView:0x100210ed0](有效)

1 个答案:

答案 0 :(得分:1)

每个UIView都有constraints[NSLayoutConstraint]的数组:

  1. 限制视图的大小(例如.Height);
  2. 将视图限制在其子视图之一;或
  3. subviews中的两个约束到彼此。
  4. 删除视图后,其constraints中的所有约束都将被删除或停用。然后检查其超级视图及其超级视图的超视图的约束,并删除或停用涉及要删除的视图的任何约束。