From the UIStackView Class Reference
In removeArrangedSubview:
To prevent the view from appearing on screen after calling the stack’s removeArrangedSubview: method, explicitly remove the view from the subviews array by calling the view’s removeFromSuperview method.
In arrangedSubview:
Whenever an arranged view’s removeFromSuperview method is called, the stack view removes the view from its arrangedSubview array
From these, it seems that calling just removeFromSuperview is enough to remove a subview and I've been using it like that without problems. I also confirmed the behavior by logging the count of the arrangedSubviews array when removeFromSuperview is called.
A lot of tutorials and comments here on S/O however, say to call both. Is there a reason for this? Or do people just do it because the documentation says so?
Managing Subviews To remove an arranged subview that you no longer want around, you need to call removeFromSuperview() on it. The stack view will automatically remove it from the arranged subview list.
To prevent the view from appearing on screen after calling the stack's removeArrangedSubview: method, explicitly remove the view from the subviews array by calling the view's removeFromSuperview() method, or set the view's isHidden property to true.
No, just call subview.removeFromSuperview()
/* Removes a subview from the list of arranged subviews without removing it as a subview of the receiver. To remove the view as a subview, send it -removeFromSuperview as usual; the relevant UIStackView will remove it from its arrangedSubviews list automatically. */ open func removeArrangedSubview(_ view: UIView)
In iOS 12.0, You need to use
stackView.arrangedSubviews[index].removeFromSuperview()
If you use removeArrangedSubview
, there is a bug where the view at the specified index removed, but the view I want to clear appears at CGPoint(x: 0, y: 0)
.
Hope this help someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With