I like to have a custom selected image when a user selects an item on the tab bar, by default it selects as blue like but would like to have a green color instead. something like below any thoughts?
To add a Tab Bar Controller to the Storyboard, select the 4 placeholders that we just created and the View Controller. Then, go to Editor, select Embed in and Tab Bar Controller. This will envelop all those scenes in a single Tab Bar Controller. Put the new Tab Bar Controller on top of the other controllers.
backgroundColor = UIColor(red:1, green:0, blue:0, alpha:1) / UITabBar. appearance(). tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) // New!! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {...}
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
In iOS 6 I have change the selected tabbatitem image like -
in tabbar controller delegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([tabBarController selectedIndex] == 0)
{
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
}
}
through this you can change your image.
Or you can use directly in your view controllers init(or ViewWillAppear) method, like
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
i hope this'll help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With