Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wh1pch81n/bd2b010b9cf1fb4427d5376690ec8718 to your computer and use it in GitHub Desktop.
Save wh1pch81n/bd2b010b9cf1fb4427d5376690ec8718 to your computer and use it in GitHub Desktop.
An alternative implementation for setting the magnifying glass color. You need to add this class to the scene dock of your View Controller in interface builder and connect the IBOutlet to the UISearchBar instance. The color for the component can be set on the object in storyboard. The although this is more work than the other approach you have t…
class UISearchBarMagnifyingGlass: NSObject {
@IBOutlet weak var searchBar: UISearchBar! {
didSet {
if let _color = color, let _searchBar = searchBar {
for i in _searchBar.subviews.first!.subviews {
if let textField = i as? UITextField,
let imageView = textField.leftView as? UIImageView,
let image = imageView.image
{
let coloredImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
imageView.image = coloredImage
imageView.tintColor = _color
}
}
}
}
}
@IBInspectable var color: UIColor?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment