Skip to content

Instantly share code, notes, and snippets.

@gbertb
Forked from myell0w/UIView+Hierarchy.swift
Created June 16, 2018 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbertb/896eaba6a7e3d4725d2b45eb934ca315 to your computer and use it in GitHub Desktop.
Save gbertb/896eaba6a7e3d4725d2b45eb934ca315 to your computer and use it in GitHub Desktop.
Traverse View Hierarchy Upwards
extension UIView {
func findFirstSuperview<T>(ofClass viewClass: T.Type, where predicate: (T) -> Bool) -> T? where T: UIView {
var view: UIView? = self
while view != nil {
if let typedView = view as? T, predicate(typedView) {
break
}
view = view?.superview
}
return view as? T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment