-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintableSubviewHierarchy.swift
55 lines (46 loc) · 1.83 KB
/
PrintableSubviewHierarchy.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// PrintableSubviewHierarchy.swift
// GVOSCollection
//
// Created by Yan Ren on 11/27/23.
//
import UIKit
extension ZTGGViewController {
func subviewsPrint(of inputView: UIView) -> [UIView] {
var viewCollection = [UIView]()
func recursivePrinter(currLevel: Int, inputView: UIView) {
if currLevel == 0 {
viewCollection.append(inputView)
print("\(inputView.classForCoder)")
}
let spacer = String(repeating: String(repeating: " ", count: 4), count: currLevel + 1)
for subview in inputView.subviews {
print(spacer + "|--> \(subview.classForCoder)")
viewCollection.append(subview)
guard !subview.subviews.isEmpty else { continue }
recursivePrinter(currLevel: currLevel + 1, inputView: subview)
}
}
recursivePrinter(currLevel: 0, inputView: inputView)
return viewCollection
}
}
// MARK: - de-integrate into separate functions
func recursivePrinter(currLevel: Int, inputView: UIView, viewCollection: inout [UIView]) {
if currLevel == 0 {
viewCollection.append(inputView)
print("\(inputView.classForCoder)")
}
let spacer = String(repeating: String(repeating: " ", count: 4), count: currLevel + 1)
for subview in inputView.subviews {
print(spacer + "|--> \(subview.classForCoder)")
viewCollection.append(subview)
guard !subview.subviews.isEmpty else { continue }
recursivePrinter(currLevel: currLevel + 1, inputView: subview, viewCollection: &viewCollection)
}
}
func subviewsPrint(of inputView: UIView) -> [UIView] {
var viewCollection = [UIView]()
recursivePrinter(currLevel: 0, inputView: inputView, viewCollection: &viewCollection)
return viewCollection
}