Skip to content

Instantly share code, notes, and snippets.

@vinczebalazs
Last active January 25, 2020 22:47
Show Gist options
  • Save vinczebalazs/51cb4c91bdcdcef72ff4c5852b70d0f6 to your computer and use it in GitHub Desktop.
Save vinczebalazs/51cb4c91bdcdcef72ff4c5852b70d0f6 to your computer and use it in GitHub Desktop.
UIView+Superview
extension UIView {
func superview<T>(of type: T.Type) -> T? {
return superview as? T ?? superview.flatMap { $0.superview(of: type) }
}
}
// Example usage:
func textFieldDidBeginEditing(_ textField: UITextField) {
// Get the cell containing the textfield.
if let cell = textField.superview(of: TextFieldTableViewCell.self) {
cell.toggle(isHighlighted: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment