Skip to content

Instantly share code, notes, and snippets.

@646b
Last active January 18, 2016 20:47
Show Gist options
  • Save 646b/53a49147a35d4224bb7c to your computer and use it in GitHub Desktop.
Save 646b/53a49147a35d4224bb7c to your computer and use it in GitHub Desktop.
Useful UIView extensions
extension UIView {
/**
Searches for superview with specified type in view hierarchy
let cell = button.superviewOfType(UITableViewCell.self)
- Parameter type: Type of superview
- Returns: A superview of specified type
*/
func superviewOfType<T>(type: T.Type) -> T? {
var currentView = self
while let superview = currentView.superview {
if superview is T {
return superview as? T
}
currentView = superview
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment