Skip to content

Instantly share code, notes, and snippets.

@ardarda
Forked from a2/UIViewExtensions.swift
Created June 16, 2018 20:52
Show Gist options
  • Save ardarda/464dc8023f8d6b755b676d5b7331bec0 to your computer and use it in GitHub Desktop.
Save ardarda/464dc8023f8d6b755b676d5b7331bec0 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