Why does't the selectedImage of a tab bar show up properly? - ios

I'm trying to specify an image for the selectedImage of a UITabBar tab. This image is contained in the Asset Catalogs folder. The image is called secondActive.
The image that shows up in the Simulator, however, does not represent the image. It's just a blue square. When the tintColor changes, the color of the square changes too.
I'm specifying the selectedImage of the tab in the User Defined Runtime Attributes and in the Attributes Inspector.
Why does't the selectedImage of a tab bar show up properly?

This happens because your images do not contain alpha transparency. By default, bar and tab bar items use images in template rendering mode. You can tell the system to load it in original form, but then changing tint will not work (image will have the pixel color given in the image itself). Best to reauthor your images to include transparency where needed.

Related

iOS - How to remove highlighting of selected items in Tab Bar

I am customizing the tab bar, and I don't my items to be highlighted with the tint color set to the tab bar item. I wan't the highlight to be just the image that I set for the selectedImage property of the tab bar item.
This is what it looks like now
I don't want my image to be highlighted with blue. I want it to be just the image that I set.
Please don't tell me about changing tint colors. I am not asking about tint colors here.
As per UITabBarItem documentation for init(title:image:selectedImage:) initializer:
By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with
alwaysOriginal
So, in your case:
let yourTabBarItem = UITabBarItem(title: yourTitle, image: yourImage.withRenderingMode(.alwaysOriginal), selectedImage: yourSelectedImage.withRenderingMode(.alwaysOriginal))

Gray square instead of an image in the tab bar. I use storyboard

I uploaded a picture in the project, have it in the inspector, but only bar button draws a gray square. In other examples, nothing further is required. What to do?
UITabBarItems' image property automatically sets the render mode of images to template. If you want to use a full color image as a tabbar item image you should explicitly set the render mode of the image to UIImageRenderingModeAlwaysOriginal. You can set this in a .xcassets folder by selecting the image, opening the right pane, and setting render as "Original Image". Right now the tabbar is only looking at the alpha value of the image and tinting it gray. For more information you should look at the UIKit User Interface Catalog

xcode 7.3 custom image is grayed out in storyboard

I added custom image to to assets.xcassets as 3x in xcode project. It shows fine.
Next, I added the iage to Tab Bar Item by selecting system item as custom and selected image as custom image. Also populated Bar Item image by selecting custom image.
In my storyboard, the image icon is completely grayed out in both the Tab Bar Item and Bar Item.
The image was created in Gimp as transparent image size scaled to 75x75
Can someone tell me how to fix this?
Like #luiyezheng said, this is caused by the image rendering mode.
A better way to apply the rendering mode to all items in your TabBar will be to put this code in your TabBarController viewDidLoad method:
for item in self.tabBar.items! {
item.image = item.image?.imageWithRenderingMode(.AlwaysOriginal)
item.selectedImage = item.selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
}
After iOS7, you can set whether a UIImage render using tint color of current view with imageWithRenderingMode:
AlwaysOriginal
Always draw the original image, without treating it as a template.
AlwaysTemplate
Always draw the image as a template image, ignoring its color
information.
Automatic
Use the default rendering mode for the context where the image is
used.
So what you want is:
self.barItem.image = UIImage(named: "yourImage")?.imageWithRenderingMode(.AlwaysOriginal)
By set renderingMode to AlwaysOriginal, the UIImage will always draw the original image and won't apply the template. Then you can get what you want.
Good luck:-)

Multiple tab bar icons are in selected state in iOS 7.1

In tab bar, only currently active item should be in blue colour. But in iOS 7, multiple item's image are in selected state (blue colour). Apart from iOS-7 i.e. iOS-8 and above, its working perfectly fine. PFA screenshot here
Check your image in storyboard where you have set, I think you have provide same image for both state, Selected and Normal. This should be your problem.
Provide different image for both, selected image under Tab Bar Item and image under Bar Item. See above image.
Also you can refer this link. How to change the tab bar image color for selected and unselected

How to set tabbar image color ios button?

i create a 4 button like tabbar , but image show as colored ,i need show images as like tabbar image color (gray) .
Thanks
Set the icon to the tabbar like this
self.tabBarItem.image = [UIImage imageNamed:#"myImage"];
Don't use
[self.tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
because then the image will not be automatically styled to the default tabbar style
You just need to set the image mask (gray, 1 color image with the transparency), not the complete image. According to the documentation
A custom icon that you provide for a toolbar, navigation bar, or tab
bar is also known as a template image, because iOS uses it as a mask
to create the icon you see in your app. It is not necessary to create
a full-color template image.
See for more info:
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/MobileHIG/IconsImages/IconsImages.html%23//apple_ref/doc/uid/TP40006556-CH14-SW1

Resources