Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shakemno/a2486ea3516c79c16b2bb905d9b077f6 to your computer and use it in GitHub Desktop.
Save shakemno/a2486ea3516c79c16b2bb905d9b077f6 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)
}
}
// 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