How to find subview recursively in Swift

Issue #460

1
2
3
4
5
6
7
8
9
10
11
12
13
extension UIView {
func findRecursively<T: UIView>(type: T.Type, match: (T) -> Bool) -> T? {
for view in subviews {
if let subview = view as? T, match(subview) {
return subview
} else {
return view.findRecursively(type: type, match: match)
}
}

return nil
}
}

Comments