Last active
August 15, 2016 02:27
-
-
Save wh1pch81n/b03b2ac83d2d79bfa087625e43f95409 to your computer and use it in GitHub Desktop.
Ever want to change the color of the magnifying glass in the UISearchBar? Well now you can and you can change its color in interface builder!
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 UISearchBar { | |
/** An easy way to set the magnifying glass color in interface builder | |
It is recommended to set this in interface Builder, | |
However if you want to do it programatically you can do it like this: | |
``` | |
let searchBar = UISearchBar() | |
searchBar.magnifyingGlassColor = UIColor.red | |
``` | |
*/ | |
@IBInspectable var magnifyingGlassColor: UIColor { | |
get { | |
return getSearchBarLeftViewImageView()?.tintColor ?? UIColor() | |
} | |
set { | |
if let imageView = getSearchBarLeftViewImageView(), | |
let image = imageView.image | |
{ | |
let coloredImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate) | |
imageView.image = coloredImage | |
imageView.tintColor = newValue | |
} | |
} | |
} | |
func getSearchBarLeftViewImageView() -> UIImageView? { | |
for i in self.subviews.first!.subviews { | |
if let textField = i as? UITextField, | |
let imageView = textField.leftView as? UIImageView | |
{ | |
return imageView | |
} | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment