I have a button with an image that I set like this
playPauseAudioBtn.setImage(UIImage(named:"icon-play-video"), forState: .Normal)
But looks with inverted colors, I mean: the white part of the button is black and the black part is white
UPDATE
Button type is Custom
UPDATE 2
If I set the image using the inspector it looks good. But when I modify it by code looks bad again.
UPDATE 3
I tried this, but it doesn't works:
playPauseAudioBtn.imageView?.image = UIImage(named: "icon-play-video")
UPDATE 4
Here's an example:
Before, it looks correct
After, it doesn't looks correct
If you are using an Asset Catalog for your images, ensure that the Render As property is not set to Template Image.
Related
I want to add special icon with 2 color in the tab bar but the app detect just one color I used lots of single color icons and there is no problem with them but this icon won't show as I want to be
here is the image of that Icon
the tabor background is white
try to use .jpg of the image in place of .png
If you are using image assets then change the render option as Original Image instead of Default.
Otherwise change the tint color of tabor to clear.
Try to set color you want using this code.
UITabBar.appearance().tintColor = .red
Or you can tell the system to keep the original rendering mode, so it does not use default colors. Set image like that.
... = UIImage(named:"myImage.png")?.withRenderingMode(.alwaysOriginal)
One of my apps has three UIButtons on it, and they use images that I got from icons8. In the code, I change the tint color and it works fine. Whatever I set the tint color to is what color the button image becomes that color.
Today I wanted to add a fourth button to mute sound. I downloaded the images that I wanted from icons8, added the button and set it up like I have the rest of the buttons set up. The new button is not changing colors like the rest of them. I'm certain that it has to do with the image, because if I select a different image, it changes color. I also tried using the new image on one of the older buttons and it would not change color.
My question - is there anything special that needs to be done to an image to make it work with tint color?
Try setting the image to render as a template image. You can do this in your .xcasset folder selecting the image set, opening the attributes inspector and setting render as to "template image."
What I did was follow the #beyowulf instructions and on the code I just did this
yourBtn.tintColor = .white (or other color)
this worked for me
In my application I've added this searchbar:
I tried to customize the button and it works great, I'm using
searchbar.setImage(UIImage(named: "Filter_icon"), forSearchBarIcon: .Bookmark, state: .Normal)
and the button icon has changed correctly. Now I need to change the color of the icon? How can I do?
Thanks in advance.
Try setting your image rendering mode in your asset catalog.
iOS chooses the rendering mode depending of the context, an UIImageView image rendering mode will be set to original, when a UIButton image rendering mode will be set to template.
You can force its behavior in the attribute inspector.
First, select the image in your asset catalog. You'll see
on the bottom of the inspector Render as.
Template image will override all colors to the tint of the view.
Select Original image to keep the color of your image.
Default is the behavior I described above.
Of course your image should be orange (like my tire is green).
The simplest way is change your icon color and render as original color
searchBar.setImage(UIImage(named: "Filter_icon")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), forSearchBarIcon: .Bookmark, state: .Normal)
searchBar.showsBookmarkButton = true
I finally solved my problem setting two different bookmark buttons. One blue and one gray like this:
searchbar.setImage(UIImage(named: "Filter_icon_blue"), forSearchBarIcon: .Bookmark, state: .Normal)
searchbar.setImage(UIImage(named: "Filter_icon_gray"), forSearchBarIcon: .Bookmark, state: .Normal)
I create two image_set in the assets.xcassets and I change this programmatically.
Instead of using the tint color to indicate when a tab is selected, I just want to use the original images. These are the images I'm using:
Default:
Selected:
I added the images in the Storyboard, and in my code to setup the TabBarController I have the following:
let manageItem = tabBar.items?[1]
manageItem?.image?.imageWithRenderingMode(.AlwaysOriginal)
manageItem?.selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
But every time I build and run, I'm still getting the blue color when selected. Also, it seems to be altering the selected image. Here's what it looks like:
Not selected (second tab):
Selected:
Why is it not using the original images?
As far as I recall imageWithRenderingMode returns new image, so you should rather use it like this :
let manageItem = tabBar.items?[1]
manageItem?.image = manageItem?.image?.imageWithRenderingMode(.AlwaysOriginal)
manageItem?.selectedImage = manageItem?.selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
The bar item images I prepared are about 35*35
and I use the Sketch to export the [1x & 2x] size images;
(I can't insert any images for instances at here because of my zero reputation//Sorry about this!)
Then it displays a blue square :<
the function and title of Item is OK
BUT just the icons are wrong
Could someone tells me how to fix it!
Appreciate!!!
I figure it out!
There is nothing wrong with your code or setting
It's about your IMAGEs
You can just look at the apps on your phone.(almost)Any tab bar item icons are in gray.
So the Icon will just display their UN Hollow parts in gray!!
[Additionally,according to official API,they wrote like this:
By default, unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal.
]
Consequently the solution is : USE Sketch to make your instance's icon heart-part to be hollow
You need to add #3x image too.