Changing selectedImage on UITabBarItem in Swift
The previous answer does not fully work.
I have to set the new UIImage
's UIImageRenderingMode
to AlwaysOriginal
, this solves my situation.
code bellow:
import UIKit
class MainTab: UITabBarController {
override func viewDidLoad() {
var tabBar = self.tabBar
var homeSelectImage: UIImage! = UIImage(named: "firstPageSelected")?.imageWithRenderingMode(.AlwaysOriginal)
var qaSelectImage: UIImage! = UIImage(named: "Q&ASelected")?.imageWithRenderingMode(.AlwaysOriginal)
var mySelectImage: UIImage! = UIImage(named: "myBagSelected")?.imageWithRenderingMode(.AlwaysOriginal)
(tabBar.items![0] as! UITabBarItem ).selectedImage = homeSelectImage
(tabBar.items![1] as! UITabBarItem ).selectedImage = qaSelectImage
(tabBar.items![2] as! UITabBarItem ).selectedImage = mySelectImage
tabBar.tintColor = UIColor.greenColor()
}
}
Hope that work for you
How to programmatically change Tab Bar Item image and font when selected?
Programmatically set selected UITabbarItem
's image:
let tabBarItem = UITabBarItem(title: "title", image: UIImage(named: "defaultImage"), selectedImage: UIImage(named: "selectedImage"))
You can't as easily set selected UITabbarItem
's font though. You'd need to create your own UITabBarController
as shown in this SO thread.
How to customize tintColor and resize UITabBarItem
EDIT:
You are missing breaks in your switch statement:
switch item.tag{
Also, you are doing a switch on the tag and I don't see anywhere you have tagged them accordingly in your code. You should get the index of the item instead.
I am not a Swift coder, this is how you do it in Objective-C to give you a hint:
NSInteger indexOfTab = [[self.tabBar items] indexOfObject:item];
Then you do your switch statement of indexOfTab.
Here is the Swift version.:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the selected index is : \(tabBar.items.index(of: item))")
}
If you want to individually change the "tintColor" , you should set a custom selectedImage instead.
Beware:
By default, unselected and selected images are automatically created
from the alpha values in the source images. To prevent system
coloring, provide images with alwaysOriginal.
As far as the documentation goes, there are no "tintColor" property for a UITabBarItem.
However, the UITabBar itself has a tintColor property. But this is not setting anything individually.
Tint Color
You can specify a custom tint color for the bar background using the
Tint (barTintColor) field. The default background tint color is white.Use the Image Tint (selectedImageTintColor) field to specify the bar
item’s tint color when that tab is selected. By default, that color is
blue.
Regarding your resize methods, you should resize your original image instead, or check this question if it does fit your needs. However, the UITabBar and UITabBarItem customizations are limited to what you can read in the documentations.
If you want to further customize things individually, I suggest you search for or create a custom solution instead.
Changing tab bar item image and text color iOS
From UITabBarItem class docs:
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
UIImageRenderingModeAlwaysOriginal.
The clue is not whether you use UIImageRenderingModeAlwaysOriginal, the important thing is when to use it.
To prevent the grey color for unselected items, you will just need to prevent the system colouring for the unselected image. Here is how to do this:
var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem
As you can see, I asked iOS to apply the original color (white, yellow, red, whatever) of the image ONLY for the UNSELECTED state, and leave the image as it is for the SELECTED state.
Also, you may need to add a tint color for the tab bar in order to apply a different color for the SELECTED state (instead of the default iOS blue color). As per your screenshot above, you are applying white color for the selected state:
self.tabBar.tintColor = UIColor.whiteColor()
EDIT:
Change UITabBarItem image on selection (Xcode 12, Swift)
Figured it out, when setting the image, set the image
and selectedImage
.
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
case 0:
if publishAudio == false {
item.image = UIImage(named: "mute_off")
item.selectedImage = UIImage(named: "mute_off")
} else {
item.image = UIImage(named: "mute_on")
item.selectedImage = UIImage(named: "mute_on")
}
default:
preconditionFailure("Unexpected item tag: \(item.tag)")
}
}
Setting Selected Image in Tab Bar Controller with Storyboard
You can use storyboard to set selected image of tabbar. I tried and it worked for me.
Select the UITabbarItem and add a run-time attribute 'selectedImage', select Type as 'Image' and give the name of your image as its value.
I am using XCode 6.0 and my minimum deployment target is iOS 8.0.
(Swift) Tab Bar Item: Custom Selected Image with Rendering asOriginal is Not Showing
I extend UITabBarController:
extension UITabBarController {
func updateTabBarItem(tab: Int, image: UIImage?) {
guard let tabItems = tabBar.items, tab < tabItems.count && tab >= 0
else { return }
let tabItem = tabItems[tab]
tabItem.image = image?.withRenderingMode(.alwaysOriginal)
tabItem.selectedImage = tabItem.image
}
}
This will help to access the tabBar.items without loading any view controllers (except the first view controller of the tab 0).
class MyViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.updateTabBarItem(tab: 1, image: UIImage(named: "selected-image")) // update the second tab's image here (just for example)
}
}
For example, if you want to change the tab 2 selected image, make a break point on viewDidLoad: on the second view controller, you will find the break point doesn't hit, that's why the tab item's image wouldn't be updated.
Is there a way to use a custom selected image for UITabBarItem?
Just found my solution. Basically, I subclassed UITabItem and set this in the navigation controller:
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"Events" image:[UIImage imageNamed:@"tabIcon.png"] tag:0];
tabItem.customHighlightedImage=[UIImage imageNamed:@"tabIconSelected.png"];
self.tabBarItem = tabItem;
[tabItem release];
tabItem=nil;
}
Here's what the CustomTabBarItem class looks like:
@interface CustomTabBarItem : UITabBarItem
{
UIImage *customHighlightedImage;
}
@property (nonatomic, retain) UIImage *customHighlightedImage;
@end
implementation:
#import "CustomTabBarItem.h
@implementation CustomTabBarItem
@synthesize customHighlightedImage;
- (void)dealloc {
[customHighlightedImage release];
customHighlightedImage=nil;
[super dealloc];
}
-(UIImage *)selectedImage {
return self.customHighlightedImage;
}
@end
Set Tab Bar image at runtime
Ok I fixed it!
Subclass UITabBarController
In viewDidLoad of your subclass set self.tabBar.items?[0].image and self.tabBar.items?[0].selectedImage to your desired image. Here I am settings the first VC's tab bar item but you can set any with the index or loop through and set all.
Related Topics
How to Detect Which Annotation Was Selected in Mapview
How to Get Label Name from Button
Get Center Coordinates from Mapkit and Display in Uilabel
How to Draw Geojson in Apple Maps as Overlay Using Swift 3
How to Get All Keys and Values into Separate String Arrays from Nsdictionary in Swift
Avmutablevideocomposition Output Video Shrinked
How to Get the Today's and Tomorrow's Date in Swift 4
Swift Spritekit Playing Audio in the Background
Add Progress to File Uploading Using Alamofire
Setting Custom Http Headers in Alamofire in iOS 7 Not Working
How to Check If Device Orientation Is Landscape Left or Right in Swift
How to Get All the Sundays in Array of Date iOS
How to Change Searchbar Border Color
Spritekit/Swift - How to Check Contact of Two Nodes When They Are Already in Contact
How to Draw Text in Center to Uiimage