Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIStackView : Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?

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?

like image 855
robola Avatar asked May 30 '16 12:05

robola


People also ask

How do I remove all Subviews from Uistackview?

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.

How do I remove items from Stackview?

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.


2 Answers

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) 
like image 142
zurakach Avatar answered Oct 06 '22 15:10

zurakach


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.

like image 45
Changnam Hong Avatar answered Oct 06 '22 15:10

Changnam Hong