Last active
November 10, 2022 11:54
-
-
Save ukiyoevega/9b8b1fdc5fd740bf302890d72f34d8f5 to your computer and use it in GitHub Desktop.
print view hierarchy in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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