Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
func logViewHierarchy(view: UIView) -> String {
var viewsPrinted = Set<UIView>()
var result: String = ""
func printViews(_ view: UIView, level: Int) {
guard !viewsPrinted.contains(view) else { return }
let padString = String(repeating: " | ", count: level)
result += ("\(padString) \(type(of: view))\n")
viewsPrinted.insert(view)
for subview in view.subviews {
printViews(subview, level: level + 1)
}
}
printViews(view, level: 0)
return result
}
let v = UIView()
for i in 0...10 {
let view = UIView()
if i % 2 == 0 {
view.addSubview(UIView())
}
v.addSubview(view)
}
let result = logViewHierarchy(view: v)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.