Skip to content

Instantly share code, notes, and snippets.

@kishorek
Created November 9, 2017 06:15
Show Gist options
  • Save kishorek/eb0f12aeaba401753ed93c4d7bd3c22b to your computer and use it in GitHub Desktop.
Save kishorek/eb0f12aeaba401753ed93c4d7bd3c22b to your computer and use it in GitHub Desktop.
Remove all subviews from UIView
extension UIView {
// Recursive remove subviews and constraints
func removeSubviews() {
self.subviews.forEach({
if !($0 is UILayoutSupport) {
$0.removeSubviews()
$0.removeFromSuperview()
}
})
}
// Recursive remove subviews and constraints
func removeSubviewsAndConstraints() {
self.subviews.forEach({
$0.removeSubviewsAndConstraints()
$0.removeConstraints($0.constraints)
$0.removeFromSuperview()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment