Last active
January 25, 2020 22:47
-
-
Save vinczebalazs/51cb4c91bdcdcef72ff4c5852b70d0f6 to your computer and use it in GitHub Desktop.
UIView+Superview
This file contains hidden or 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
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