0
votes

Assume that there has two subviews in a containView.

enter image description here

Don't consider the width and height constraint,just think they are equal to a constant.

Subview1:

has a leading and top constraint to superview.And a 'horizontol constraint' to subview2.

Subview2:

has a trailing and top constaint to superview.


As I known, if i hide the subview2,the 'horizontol constraint' still exist.But if i remove it from containView.What does the project do to remove the 'horizontal constaint'?

I think the subview2 is like a root-node of muti-tree.If it was removedFromSuperview,the root-node retain count is 0,then child node's(like constraint) retaincount released... Another idea is when remove subviews,uiview will traverse it's constraint array and remove some constraint related to nil view.

Am i right?Can someone give me opinion?


I've tried this,

enter image description here

And here is the log behind remove and removed:

<__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)>
)

This line was removed after subview2 removed:

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

1
Are you asking what to do about constraints if you remove subview2? I really don't know what retain counts have to do with this.NRitH
@NRitH No.I just want to know the principle that how did the horizontal constraint dismiss...I don't know my idea is right or wrong纪忠懿
Are you doing it programmatically?Sofeda
@SMi Yes,see my update纪忠懿

1 Answers

1
votes

Each UIView has an array (constraints)of [NSLayoutConstraint]s that:

  1. Constrain the view's size (e.g. .Height);
  2. Constrain the view to one of its subviews; or
  3. Constrain two of its subviews to each other.

When a view is deleted, all of the constraints in its constraints are deleted or deactivated. Then its superview's, and its superview's superview's, constraints are examined, and any that involve the view to be deleted are deleted or deactivated.