Skip to content

Instantly share code, notes, and snippets.

@Geri-Borbas
Created July 26, 2020 01:33
Show Gist options
  • Select an option

  • Save Geri-Borbas/933c444a0f87a0f71e4c568c4cbce7b1 to your computer and use it in GitHub Desktop.

Select an option

Save Geri-Borbas/933c444a0f87a0f71e4c568c4cbce7b1 to your computer and use it in GitHub Desktop.
Search ancestral view hierarchy for a given `UIView` type.
extension UIView {
/// Search ancestral view hierarchy for the given view type.
func searchViewAnchestorsFor<ViewType: UIView>(
_ onViewFound: (ViewType) -> Void
) {
if let matchingView = self.superview as? ViewType {
onViewFound(matchingView)
} else {
superview?.searchViewAnchestorsFor(onViewFound)
}
}
}
// Usage.
view.searchViewAnchestorsFor { (scrollView: UIScrollView) in
// Do something with the scroll view.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment