Last active
July 20, 2022 14:09
-
-
Save jamesujeon/27b8121a83cd4e70bca39d60a35db8fe to your computer and use it in GitHub Desktop.
Get the ancestor view of the specific type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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