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.
Related
I've created a custom button and set two images, one is for normal, and the other is for the selected mode. But the voice-over always says the normal image name text when the button is not selected. I've tried a lot but could not disable it.
When I disable the button imageView accessibility it is not working.
button.imageView?.isAccessibilityElement = false
When I disable the button accessibility, the voice-over is not working in accessibility mode.
button.isAccessibilityElement = false
If I remove the '.normal' mode image then it works, but normal mode image functionality is not considered/worked there. I'm surfing a lot. Help anyone and thanks in advance.
Code:
self.setImage(UIImage.init(named: imageName1), for: .normal)
self.setImage(UIImage.init(named: imageName1), for: .selected)
Simply, It is not possible. You can use an accessibility label instead.
button1.imageView?.accessibilityLabel = "Radio button deselected"
You have to set button accessibilityLabel as empty string.
button1.accessibilityLabel = ""
VoiceOver will stop saying image name for any of the state.
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)
I need to know if there is a way to change the tab bar item title / text (not the image because it is working perfectly fine just by specifying tintColor) during actual user interaction? Like changing it's color when a certain button is tapped.
Currently the unselected / inactive tab bar item text has no color. Is there a way to specify it's color either through storyboard or code?
I know that the tab bar item title / text color can be specified using :
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal)
and
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .selected)
but this only applies if the the tab is not yet rendered. Calling the said methods after the tab bar controller gets created doesn't do anything. Please do note that my question is for iOS 9 and below since changing the tab bar tint color for selected and unselected is quite easy in iOS 10.
I got it. It turns out specifying attributes individually will work rather than globally.
tabBar.items?[index].setTitleTextAttributes([NSForegroundColorAttributeName: normalColor], for: .normal)
tabBar.items?[index].setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor], for: .selected)
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
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.