Remove views in UIstackview swift Remove views in UIstackview swift ios ios

Remove views in UIstackview swift


In Swift 5.4

removeArrangedSubview method removes the provided view from the stack’s arrangedSubviews array. The view’s position and size will no longer be managed by the stack view. However, this method does not remove the provided view from the stack’s subviews array; therefore, the view is still displayed as part of the view hierarchy.

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.

So:

myStackView.removeArrangedSubview(myView)myView.removeFromSuperview()

ExtendedIf you have a series of arranged subview, and you want to clean them, You can also create an extension:

extension UIStackView {        func removeFully(view: UIView) {        removeArrangedSubview(view)        view.removeFromSuperview()    }        func removeFullyAllArrangedSubviews() {        arrangedSubviews.forEach { (view) in            removeFully(view: view)        }    }    }


If you want to hide a view within a stack view, all you have to do is set the contained view’s hidden property to true and the stack view handles the rest.

So what you must call as far as I understood from your code is the following:

subview.hidden = true


By my experience backed by actually testing the implementation (😉), all you have to do to remove subview from stackView is call removeFromSuperview() method on every subview of the stackView:

stackView.subviews.forEach { (view) in    view.removeFromSuperview()}

This is how it looks in console:

(lldb) po stackView.arrangedSubviews▿ 3 elements  - 0 : <UIStackView: 0x7f840c8297d0; frame = (0 0; 375 95); layer = <CATransformLayer: 0x600003f48c80>>  - 1 : <UIStackView: 0x7f840c82f5e0; frame = (0 125; 375 95); layer = <CATransformLayer: 0x600003f54520>>  ▿ 2 : <UIView: 0x7f840b3c93a0; frame = (0 250; 375 47); opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x600006bdae20>; layer = <CALayer: 0x600003f4a7e0>>(lldb) po stackView.subviews▿ 3 elements  - 0 : <UIStackView: 0x7f840c8297d0; frame = (0 0; 375 95); layer = <CATransformLayer: 0x600003f48c80>>  - 1 : <UIStackView: 0x7f840c82f5e0; frame = (0 125; 375 95); layer = <CATransformLayer: 0x600003f54520>>  ▿ 2 : <UIView: 0x7f840b3c93a0; frame = (0 250; 375 47); opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x600006bdae20>; layer = <CALayer: 0x600003f4a7e0>>

Then you remove all subviews:

(lldb) expression stackView.subviews.forEach { (view) in view.removeFromSuperview() }

And the result:

(lldb) po stackView.subviews0 elements(lldb) po stackView.arrangedSubviews0 elements

You can make extension to UIStackView to save you some typing in the future:

extension UIStackView {    func removeAllSubviews() {        subviews.forEach { (view) in            view.removeFromSuperview()        }    }}


matomo