Skip to content

Instantly share code, notes, and snippets.

@yasirmturk
Created April 15, 2020 07:49
Show Gist options
  • Save yasirmturk/0b47d18a30722902a9a4aaad05d1794a to your computer and use it in GitHub Desktop.
Save yasirmturk/0b47d18a30722902a9a4aaad05d1794a to your computer and use it in GitHub Desktop.
Remove all arranged subviews from UIStackView properly
import UIKit
extension UIStackView {
@discardableResult
func removeAllArrangedSubviews() -> [UIView] {
return arrangedSubviews.reduce([UIView]()) { $0 + [removeArrangedSubViewProperly($1)] }
}
func removeArrangedSubViewProperly(_ view: UIView) -> UIView {
removeArrangedSubview(view)
NSLayoutConstraint.deactivate(view.constraints)
view.removeFromSuperview()
return view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment