Skip to content

Instantly share code, notes, and snippets.

@durul
Last active July 7, 2018 18:15
Show Gist options
  • Save durul/9ba2d8629b408b04c0c45670d9719695 to your computer and use it in GitHub Desktop.
Save durul/9ba2d8629b408b04c0c45670d9719695 to your computer and use it in GitHub Desktop.
List out all the subviews in a UIViewcontroller
extension UIView {
private var viewInfo: String {
return "\(classForCoder), frame: \(frame))"
}
private func subviews(parentView: UIView, level: Int = 0, printSubviews: Bool = false) -> [UIView] {
var result = [UIView]()
if level == 0 && printSubviews {
result.append(parentView)
print("\(parentView.viewInfo)")
}
for subview in parentView.subviews {
if printSubviews {
print("\(String(repeating: "-", count: level))\(subview.viewInfo)")
}
result.append(subview)
if subview.subviews.count != 0 {
result += subviews(parentView: subview, level: level+1, printSubviews: printSubviews)
}
}
return result
}
var allSubviews: [UIView] {
return subviews(parentView: self)
}
func printSubviews() {
_ = subviews(parentView: self, printSubviews: true)
}
}
//USAGE
view.printSubviews()
print("\(view.allSubviews.count)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment