Created
July 26, 2020 01:33
-
-
Save Geri-Borbas/933c444a0f87a0f71e4c568c4cbce7b1 to your computer and use it in GitHub Desktop.
Search ancestral view hierarchy for a given `UIView` type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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