Skip to content

Instantly share code, notes, and snippets.

@nkukushkin
Last active July 14, 2022 22:12
Show Gist options
  • Save nkukushkin/8972b9c7db89a177384df1ed6ca0da93 to your computer and use it in GitHub Desktop.
Save nkukushkin/8972b9c7db89a177384df1ed6ca0da93 to your computer and use it in GitHub Desktop.
Recursive search for the first superview matching certain criteria.
extension UIView {
func firstSuperview<T>(where predicate: (T) -> Bool) -> T? where T: UIView {
if let superview = superview as? T, predicate(superview) {
return superview
}
return superview?.firstSuperview(where: predicate)
}
}
@nkukushkin
Copy link
Author

Example:

view.firstSuperview(where: { (view: UITableView) in view.isEditing })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment