Skip to content

Instantly share code, notes, and snippets.

@briscula
Forked from myell0w/UIView+Hierarchy.swift
Created June 14, 2018 08:43
Show Gist options
  • Select an option

  • Save briscula/c6caa0466c4ef9504c636b720d6a2cc5 to your computer and use it in GitHub Desktop.

Select an option

Save briscula/c6caa0466c4ef9504c636b720d6a2cc5 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