Skip to content

Instantly share code, notes, and snippets.

@a2
Last active June 18, 2018 11:12
Show Gist options
  • Save a2/7409f8b76dda76be6dc63fe7685b3fbf to your computer and use it in GitHub Desktop.
Save a2/7409f8b76dda76be6dc63fe7685b3fbf to your computer and use it in GitHub Desktop.
import UIKit
extension UIView {
var superviews: UnfoldFirstSequence<UIView> {
return sequence(first: self) { view in view.superview }
}
func enclosingView(where predicate: (UIView) -> Bool) -> UIView? {
return superviews.first(where: predicate)
}
func enclosingView<T>(type: T.Type) -> T? where T: UIView {
return superviews.first(where: { $0 is T }) as! T?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment