Skip to content

Instantly share code, notes, and snippets.

@jamesujeon
Last active July 20, 2022 14:09
Show Gist options
  • Save jamesujeon/27b8121a83cd4e70bca39d60a35db8fe to your computer and use it in GitHub Desktop.
Save jamesujeon/27b8121a83cd4e70bca39d60a35db8fe to your computer and use it in GitHub Desktop.
Get the ancestor view of the specific type
import UIKit
extension UIView {
func ancestorView<T: UIView>() -> T? {
var view = superview
while view != nil && !(view is T) {
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