-
-
Save gbertb/896eaba6a7e3d4725d2b45eb934ca315 to your computer and use it in GitHub Desktop.
Traverse View Hierarchy Upwards
This file contains 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 { | |
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