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)
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
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.
Is there a way so that you can set both image and title of UISegmentControl simultaneously, so that image appear next to the title , just like image appear next to title in UIButton.
I am trying but if I set image of Selected segment of UISegmentControl then title disappears and if I set title of Selected segment of UISegmentControl then Image disappears, I want to set both at a time.
The official documentation for -setImage:forSegmentAtIndex: says
A segment can only have an image or a title; it can’t have both. There is no default image.
So, no, there is no way to do what you want using the image and title properties.
However, there are a few options to accomplish the objective in different ways.
if you are going to use the same image for each segment, you could use the appearance methods to set the background and then use the method -setContentOffset:forSegmentAtIndex: to move the title to the side of the background image.
you could create an image that had both the icon and the text in it. This is less than ideal as a change to the text requires exporting of the entire asset again. But is an option.
the least ideal would be to fake a segmented control using buttons that you write the code for to handle state changes. There may even be some open source options that do just that, so feel free to search for them.
I have set in Storyboard tab bar item to be Custom, Image to my outline image and Selected image to my filled image but the selected image does not show up when I run the app.
It works if I create a tab bar item programmatically using
UITabBarItem(title: String?, image: UIImage?, selectedImage: UIImage?)
I use Xcode 6.1.1.
What can be wrong?
I think, it is a bug of Xcode, if you add a user defined runtime attribute with type Image and keyPath "selectedImage", you'll can set your image.
Here example of the Tabbed Application template with a custom selected image on the second tab (I use a image from the first tab):
This is more than likely because it doesn't meet the interface requirements. Tab bar items are picky, the strokes or outlines for a selected image have to be noticeably greater than its counterparts. See here specifically the notes about the stroke width. Conditionally, if it's not a 'filled' in variation of your unselected image it has a chance of not populating depending on you create the selected image in comparison.
As of Xcode 9, the functionality to set a selected image is available (and works) in interface builder.