If you ever use removeArrangedSubview() yet the view still shows up, this is an everlasting bug with UIStackView (still there as of iOS 17).

The reliable way to remove a subview is to use subview.removeFromSuperview().

Add this extension to remove all subviews reliably.

extension UIStackView {
    func removeArrangedSubviews() {
        for view in arrangedSubviews {

removeArrangedSubview vs removeFromSuperview

UIStackView documented about how it maintains consistency between the arranged views and subviews.

As documented, removeArrangedSubview() does NOT remove the subview.

Removing a view from the arrangedSubviews array doesn’t remove it as a subview. The stack view no longer manages the view’s size and position, but the view is still part of the view hierarchy, and is rendered on screen if it’s visible.

How on earth should a removed view still be visible 🤷‍♂️

Another interesting note on the order affecting the z-index:

The order of the subviews array defines the Z-order of the subviews. If the views overlap, subviews with a lower index appear behind subviews with a higher index.




Back to Home