Last active
April 26, 2016 07:15
-
-
Save isoiphone/c729437bfaa10e4450ea to your computer and use it in GitHub Desktop.
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
extension UIView { | |
func forEachSubviewOfType<V: UIView>(type: V.Type, @noescape apply block: V -> Void) { | |
for view in subviews { | |
if let view = view as? V { | |
block(view) | |
} else { | |
view.forEachSubviewOfType(V.self, apply: block) | |
} | |
} | |
} | |
} |
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
searchController.searchBar.forEachSubviewOfType(UITextField.self) { textField in | |
textField.textColor = .whiteColor() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edit: Hah! iosdevzone chimed in while I was away and said something very similar.
I think you should split up the mapping logic and the forEach logic. Then you can recur over anything you like.
Here's iosdevzone's code as an extension: