Skip to content

Instantly share code, notes, and snippets.

@ukiyoevega
Last active November 10, 2022 11:54
Show Gist options
  • Save ukiyoevega/9b8b1fdc5fd740bf302890d72f34d8f5 to your computer and use it in GitHub Desktop.
Save ukiyoevega/9b8b1fdc5fd740bf302890d72f34d8f5 to your computer and use it in GitHub Desktop.
print view hierarchy in Swift
func printAllSubviews(of view: UIView, from layer: Int) {
for _ in 0..<layer {
print(" ", separator: "", terminator: "")
}
print("\(view): ")
let subView = view.subviews
if subView.count != 0 {
subView.forEach({ (view) in
printAllSubviews(of: view, from: layer + 1)
})
}
}
let view1 = UIView()
let view2 = UIView()
view1.addSubview(view2)
let views = [UIView(), UIView(), UIView()]
views.count
views.forEach { (view) in
view2.addSubview(view)
}
view2.subviews.count
let view = view2.subviews[1]
view.addSubview(UIView())
view.subviews
printAllSubviews(of: view1, from: 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment