Last active
July 7, 2018 18:15
-
-
Save durul/9ba2d8629b408b04c0c45670d9719695 to your computer and use it in GitHub Desktop.
List out all the subviews in a UIViewcontroller
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
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