I've png images in assets I've set Render As Template image. Following is the code. Why doesn't it set the image to be white? How to fix it?
#IBOutlet weak var iconImageView: UIImageView!
iconImageView.image = UIImage(named: "ico")?.withRenderingMode(.alwaysTemplate)
iconImageView.tintColor = .white
Your code is fine and it's working perfectly. The only problem is the image is having transparency that's why color is not visible properly.
it should work . try this, almost same.
extension UIImageView {
func setImageColor(color: UIColor) {
let templateImage = self.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
self.image = templateImage
self.tintColor = color
}
}
I need the buttons to be disabled on top of Scene objects. How can i achieve that? The current code i am working is working fine but how can i get a specific child node to be transparent,
extension SCNMaterial {
convenience init(color: UIColor) {
self.init()
diffuse.contents = color
}
convenience init(image: UIImage) {
self.init()
diffuse.contents = image
}
}
let clearMaterial = SCNMaterial(color: .clear)
boxNode.materials = [clearMaterial]
Did you not get any error? SCNGeometry not SCNNode have material. try:
boxNode.geometry?.materials = [clearMaterial]
I tried this but it did not work. Maybe SCNMaterial cant use .clear
I have always used .transparency to hide/unhide node. try this:
func show(){
yourNode.geometry?.firstMaterial?.transparency = 1
}
func hide(){
yourNode.geometry?.firstMaterial?.transparency = 0
}
I have an image, inside a UIImageView, that comes with a default color, let's say a custom grey.
In my code at some point I set a different tint in this way
myImageView.image = myImageView.image!.withRenderingMode(.alwaysTemplate)
myImageView.tintColor = someCondition() ? UIColor.red : UIColor.lightGray
The fact is that I am not able to get back to the image's original grey color. My question is if there is a way to substitute the UIColor.lightGray part of my code with something telling the UIImageView not to use any tint, but its original color.
To not apply any tint you can set the image rendering mode like this:
image.renderingMode = .alwaysOriginal
In your code you could say:
let renderingMode: UIImageRenderingMode = condition ? .alwaysTemplate : .alwaysOriginal
myImageView.image = myImageView.image?.withRenderingMode(renderingMode)
I am using this approach to change/remove tint on tap
var imageView:UIImageView = {
let img = UIImageView()
img.image = someImage.withRenderingMode(.alwaysTemplate)
img.tintColor = .cyan
return img
}()
Add gesture recognizer to image view or button with action and then just assign image that is already assigned to image view but with different rendering mode
#objc func imgTap(){
imageView.image! = imageView.image!.withRenderingMode(.alwaysOriginal)
}
I don't know how to modify the original searchIcon's color.
The searchIcon is located in the leftView of the searchTextField. Since this is accessible we can easily set the tintColor for the view. The tintColor is also used for the searchIcon.
So in your code you can change the search icon to white with the following code:
searchBar.searchTextField.leftView?.tintColor = .white
Best solution I found was here
Changing the color of the icons in a UItextField inside a UISearchBar
(This solution uses the original UISearchBar's icon, no need to supply your own graphical asset)
Converted to Swift (Swift 3):
if let textFieldInsideSearchBar = self.searchBar.value(forKey: "searchField") as? UITextField,
let glassIconView = textFieldInsideSearchBar.leftView as? UIImageView {
//Magnifying glass
glassIconView.image = glassIconView.image?.withRenderingMode(.alwaysTemplate)
glassIconView.tintColor = .whiteColor
}
You can use a custom image of a white search icon, as the search bar will not modify your supplied image. To set a custom image, use this method. (Link to documentation.)
- (void)setImage:(UIImage *)iconImage
forSearchBarIcon:(UISearchBarIcon)icon
state:(UIControlState)state;
Example Code:
[searchBar setImage:[UIImage imageNamed:#"SearchIcon"]
forSearchBarIcon:UISearchBarIconSearch
state:UIControlStateNormal];
In Swift:
searchBar.setImage(UIImage(named: "SearchIcon"), for: .search, state: .normal)
This soultion worked for me on Xcode 8.2.1 in Swift 3.0. :
extension UISearchBar
{
func setPlaceholderTextColorTo(color: UIColor)
{
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = color
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = color
}
func setMagnifyingGlassColorTo(color: UIColor)
{
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let glassIconView = textFieldInsideSearchBar?.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = color
}
}
Usage example:
searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
searchController.searchBar.setMagnifyingGlassColorTo(color: mainColor)
Swift-3:
extension UISearchBar
{
func setMagnifyingGlassColorTo(color: UIColor)
{
// Search Icon
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let glassIconView = textFieldInsideSearchBar?.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = color
}
func setClearButtonColorTo(color: UIColor)
{
// Clear Button
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let crossIconView = textFieldInsideSearchBar?.value(forKey: "clearButton") as? UIButton
crossIconView?.setImage(crossIconView?.currentImage?.withRenderingMode(.alwaysTemplate), for: .normal)
crossIconView?.tintColor = color
}
func setPlaceholderTextColorTo(color: UIColor)
{
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = color
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = color
}
}
Call these extension method like this:
searchBarTextField.setMagnifyingGlassColorTo(color: yourColor)
searchBarTextField.setClearButtonColorTo(color: yourColor)
searchBarTextField.setPlaceholderTextColorTo(color: yourColor)
For just changing the color of the search icon, without altering the actual icon in Swift 3:
if let textField = self.searchBar.value(forKey: "searchField") as? UITextField,
let iconView = textField.leftView as? UIImageView {
iconView.image = iconView.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
iconView.tintColor = UIColor.red
}
Creates a result like so:
As per Federica's anwser....To do this in swift
var image: UIImage = UIImage(named: "search")!
self.searchBar.setImage(image, forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
A solution for those using interface builder. You can add a User Defined Runtime Attribute to the UISearchBar:
searchField.leftView.tintColor of type Color
You can select the desired color of the icon, can be selected from your Color Assets library to support different colors for both light and dark mode.
Since iOS 13, we now have access to many standard system icons via SF Symbols. Also, if you want to have a unified colour scheme for your search bars so that they all look the same without having to duplicate code, you can use the Appearance workflow to change them all at once. For example:
let image = UIImage(systemName: "magnifyingglass")?.withTintColor(.white, renderingMode: .alwaysOriginal)
UISearchBar.appearance().setImage(image, for: .search, state: .normal)
This code sets all search bars in the app to use the standard magnifying glass icon, in white. You can use similar code to change the 'clear' button icon, the bookmark icon and the results list icon. You just need to change .search to the appropriate enumeration value and find the system name of the appropriate icon (which you can get from the free SF System app).
Another method that uses Appearance would be to set the tint colour of all views contained within a search view, though this may have unwanted effects in some cases (i.e. changing the colour of the text cursor):
UIView.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .yellow
This will make the left icon and the text cursor yellow, though annoyingly not the 'clear' button for some reason. I prefer not to use this method as it may not be future-proof; Apple has a habit of changing things internally that can cause unexpected changes if you don't use their approved methods.
Following on from some of the answers here
If you would like to see and make the changes in storyboard first subclass UISearch bar and do as above.
However add the changes in a #IBInspectable variable and not forgetting the #IBDesignable at the top of the class and setting the searchbar subclass in the "Identity inspector"
I have added the full working subclass and code below for swift 3.0
Here you will be able to change the placeholder text, search text and magnifying glass
import UIKit
#IBDesignable
class CustomSearchBar: UISearchBar {
#IBInspectable var placeholderColor: UIColor? {
didSet {
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = placeholderColor
}
}
#IBInspectable var textColor: UIColor? {
didSet {
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = textColor
}
}
#IBInspectable var magnifyingGlassColor: UIColor? {
didSet {
if let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField,
let glassIconView = textFieldInsideSearchBar.leftView as? UIImageView {
//Magnifying glass
glassIconView.image = glassIconView.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
glassIconView.tintColor = magnifyingGlassColor
} }
}
}
Make sure that when you use the setImage API, pass an image with rendering mode of UIImageRenderingModeAlwaysTemplate. For example:
[self.searchBar setImage:[[UIImage imageNamed: #"icon-search"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
There are no adequate reasons to set your own custom image just for changing icon tint color. Don't forget about perfomance. And retrieving a value by a raw string key doesn't garantee that other coders can mistype usually calling app crashes. We're not perfect.
How about UISearchBar instance's property searchTextField (UISearchTextField)?
if let thatMysteriousLoop = yourSearchBar.searchTextField.leftView as? UIImageView {
thatMysteriousLoop.tintColor = anyColorYouWannaSet // <-- Voilà!
}
For swift 3
searchBar.setImage(UIImage(named: "location-icon-black"), for: .search, state: .normal)
My solution:
searchBar = UISearchBar()
searchBar.searchBarStyle = .minimal
searchBar.setTextColor(.white)
let textField = self.searchBar.getTextField()
let glassIconView = textField?.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = .white
searchBar.showsCancelButton = true
extension UISearchBar {
func getTextField() -> UITextField? {
return self.value(forKey: "searchField") as? UITextField
}
func setTextColor(_ color: UIColor) {
let textField = getTextField()
textField?.textColor = color
}
}
I know it is an old post but since there is a Xamarin Bug on the dark mode and I was struggling for hours, I thought I share my pretty easy solution.
iOS- custom Renderer.
[assembly:ExportRenderer(typeof(SearchBar),typeof(YourProjectSearchbarRenderer))]
public class YourProjectSearchbarRenderer : SearchBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
// iOS >13 -> Xamarin bug on darkmode,
// just shows the light icon...
this.Control.SetImageforSearchBarIcon(UIImage.FromFile("AddAnSearchIcon.ico"), UISearchBarIcon.Search, UIControlState.Normal);
}
}
}
if(IOS_7) {
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f];
}
I would like to use Apple's built-in emoji characters (specifically, several of the smileys, e.g. \ue415) in a UILabel but I would like the emojis to be rendered in grayscale.
I want them to remain characters in the UILabel (either plain text or attributed is fine). I'm not looking for a hybrid image / string solution (which I already have).
Does anyone know how to accomplish this?
I know you said you aren't looking for a "hybrid image solution", but I have been chasing this dragon for a while and the best result I could come up with IS a hybrid. Just in case my solution is somehow more helpful on your journey, I am including it here. Good luck!
import UIKit
import QuartzCore
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// the target label to apply the effect to
let label = UILabel(frame: view.frame)
// create label text with empji
label.text = "🍑 HELLO"
label.textAlignment = .center
// set to red to further show the greyscale change
label.textColor = .red
// calls our extension to get an image of the label
let image = UIImage.imageWithLabel(label: label)
// create a tonal filter
let tonalFilter = CIFilter(name: "CIPhotoEffectTonal")
// get a CIImage for the filter from the label image
let imageToBlur = CIImage(cgImage: image.cgImage!)
// set that image as the input for the filter
tonalFilter?.setValue(imageToBlur, forKey: kCIInputImageKey)
// get the resultant image from the filter
let outputImage: CIImage? = tonalFilter?.outputImage
// create an image view to show the result
let tonalImageView = UIImageView(frame: view.frame)
// set the image from the filter into the new view
tonalImageView.image = UIImage(ciImage: outputImage ?? CIImage())
// add the view to our hierarchy
view.addSubview(tonalImageView)
}
}
extension UIImage {
class func imageWithLabel(label: UILabel) -> UIImage {
UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
label.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img!
}
}