Xcode6中使用原始TabBar不显示selectedImage的处理方法

如有侵犯,请来信:oiken@qq.com


刚刚遇到UITabBar的按钮都selectedImage在StoryBoard上设置了,但实际测试显示为空

经百度得出结果:

原来Xcode6 后的设置方式变化了,selectedImage的渲染方式改变了。

解决方法:

1,代码方式设置:

转自:http://www.cocoachina.com/bbs/read.php?tid=233093

我没有进行测试,如有误,还请通知我和多多包涵。


最近有几位朋友都问我同一个问题,做iphone6适配时,使用了xcode6来运行项目,发现使用原生的tabbar上的图片不显示了。

这个问题是因为xcode6中的一些api方法被废弃了同时tabbar上图片的渲染方式发生了改变。

先看xcode6中的tabbar api方法的变更:

- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage NS_DEPRECATED_IOS(5_0,7_0,"Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal");

这个方法在IOS7中已经被废弃了,苹果建议初始化tabbarItem时使用initWithTitle:image:selectedImage:  
图片的渲染方式不使用默认而是使用UIImageRenderingModeAlwaysOriginal

UITabBarController的.m文件中代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

FirstViewController  * firstVC = [[FirstViewController alloc] init];
SecondViewController * secondVC = [[SecondViewController alloc] init];
ThirdViewController  * thirdVC = [[ThirdViewController alloc] init];
FourViewController   * fourVC = [[FourViewController alloc] init];

    NSArray * rootViewControllers = @[firstVC,secondVC,thirdVC,fourVC];
    NSMutableArray * viewControllers = [NSMutableArray array];

    for (int index=0; index<rootViewControllers.count; index++) {
        UINavigationController * navigationViewController = [[UINavigationController alloc] initWithRootViewController:[rootViewControllers  objectAtIndex:index]];
//设置UIImage的渲染方式为UIImageRenderingModeAlwaysOriginal
UIImage * normalImage = [[UIImage imageNamed:[NSString stringWithFormat:@"normal_%d.png",index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage * selectImage = [[UIImage imageNamed:[NSString stringWithFormat:@"select_%d.png",index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//不设置title时 设置空值就可以了
        navigationViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:normalImage selectedImage:selectImage];
        navigationViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6,0,-6,0);
        navigationViewController.tabBarItem.tag = index;
        [navigationViewController.tabBarItem setTitlePositionAdjustment: UIOffsetMake(0,-3)];

        [viewControllers addObject:navigationViewController];
    }
    self.viewControllers = viewControllers;
}


2,在StoryBoard里设置:

转自:http://stackoverflow.com/questions/18894985/uitabbar-not-showing-selected-item-images-in-ios-7

经过操作,这个是ok的。本文下面还有其他方式,未曾实验,仅做保存,

No answers helped fixing this issue. The main reason is that my TabBarController wasn't my RootViewController.

The solution I used for Storyboards, and I just clicked my UITabButton and I added a runtime attribute for selectedImage:

For each of the different views associated with the UITabController.



下面是完整文章,仅作保存,以备参考:

The icons show fine in ios 6 but not in ios 7. I'm setting the selected state in the viewController viewDidLoad method. When the user selects a tab bar item the image disappears.Here is my code:

UITabBar *tabBar = self.tabBarController.tabBar;
if ([UITabBar instancesRespondToSelector:@selector(setSelectedImageTintColor:)]) {
    [self.tabBarController.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setTitle:@"Home"];
[item1 setTitle:@"Calendar"];
[item2 setTitle:@"News"];
[item3 setTitle:@"My Events"];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"homeIconSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"Calendar"] withFinishedUnselectedImage:[UIImage imageNamed:@"CalendarIconSelected"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"NewsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"News"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"EventsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"Events"]];
[item1 imageInsets];
[item2 imageInsets];
[item3 imageInsets];
share improve this question
 

15 Answers

up vote 75 down vote accepted

You need to use tabBarItem initWithTitle:image:selectedImage

[[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];

in conjunction with

imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

or (to apply parent views template tint mask, this option is default for Tab bar Items unless you opt out with the above rendering mode)

imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate

here is a code sample for one tab bar item :-

UIImage *musicImage = [UIImage imageNamed:@"music.png"];
UIImage *musicImageSel = [UIImage imageNamed:@"musicSel.png"];

musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Music" image:musicImage selectedImage:musicImageSel];

Hope this helps

share improve this answer
 
3 
not working for me :( –  Rushabh Sep 21 '13 at 10:39
 
@Rushabh I need a little more info, where are you calling it from it it from Appdelegate, viewController or UITabbar/Controller Subclass or category –  gav Sep 21 '13 at 12:36
 
not working for me too .. :-( –  Amitabha Sep 23 '13 at 6:19
 
You need to explain what you mean by "Doesn't work"... –  Mostafa Torbjørn Berg Sep 27 '13 at 13:22
 
This only works for one of the images. Using this exact code unselected tab items look like the icon I set. The selected though is just a blue circle. –  Halsafar Oct 23 '13 at 20:31

Add these lines of codes in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

tabBarItem1.selectedImage = [[UIImage imageNamed:@"selectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"deselectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.title = @"xxxx";

tabBarItem2.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.title = @"xxxx";

tabBarItem3.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.title = @"xxxx";

tabBarItem4.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = @"xxxx";

return YES;
}

this works for me... and hope for the best...

share improve this answer
 
2 
This works well for iOS7, just an FYI that the method [UIImage imageWithRenderingMode] is not available on earlier SDKs. –  JBlake Dec 10 '13 at 8:47
2 
Not working for me on iOS7. Neither background image nor tabbar item's images are showing. –  Ans Apr 20 '14 at 9:43
 
Thanks! this thing gave me a headache for 2 hours..the documentation said to use the method initWithTitle:image:selectedImage:...but that was not working for me at all! Your way works great –  Josh Engelsma Jul 15 '14 at 0:15
 
welcome @JoshEngelsma..!! :-) –  Amitabha Jul 15 '14 at 8:35
 
Works well in iOS 8+ also. –  Rumin Dec 9 '14 at 11:23

No answers helped fixing this issue. The main reason is that my TabBarController wasn't my RootViewController.

The solution I used for Storyboards, and I just clicked my UITabButton and I added a runtime attribute for selectedImage:

For each of the different views associated with the UITabController.

share improve this answer
 
 
Thank you so much for this answer! –  CeceXX Jun 22 at 18:02

if you are working with storyboards you have to put the identifier : "custom" in Navigation Controller.

then :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Assign tab bar item with titles
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];

    (void)[tabBarItem1 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];
    (void)[tabBarItem2 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];
    (void)[tabBarItem3 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];

    // Change the tab bar background
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];



    return YES;
}

This works for me.

share improve this answer
 
 
Thanks a lot. I didn't had "custom" in NavigationController. –  Ricardo Mar 31 at 16:53

After spending a couple of hours trying to make my custom tabbar to work for both iOS 6 & 7, that's what worked for me...

UITabBarController *tabBarController = (UITabBarController *)[[self window] rootViewController];

UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

tabBarItem1.title = @"Home";
tabBarItem2.title = @"Map";
tabBarItem3.title = @"Weather";
tabBarItem4.title = @"Info";

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_home_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_home_black.png"]];
    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_cloud_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_cloud_black.png"]];
    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_map_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_map_black.png"]];
    [tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_info_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_info_black.png"]];
} else {
    tabBarItem1.selectedImage = [[UIImage imageNamed:@"cyexplore_home_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem1.image = [[UIImage imageNamed:@"cyexplore_home_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

    tabBarItem2.selectedImage = [[UIImage imageNamed:@"cyexplore_cloud_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem2.image = [[UIImage imageNamed:@"cyexplore_cloud_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

    tabBarItem3.selectedImage = [[UIImage imageNamed:@"cyexplore_map_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem3.image = [[UIImage imageNamed:@"cyexplore_map_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

    tabBarItem4.selectedImage = [[UIImage imageNamed:@"cyexplore_info_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem4.image = [[UIImage imageNamed:@"cyexplore_info_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
}

UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
share improve this answer
 

None of the answers worked for me - I am using MonoTouch but if you set the TintColor property of the UITabBar itself that will highlight the selected image with that color. In obj c it may be setTintColor function.

share improve this answer
 
1 
Thank you, jharr100, this resolved the issue for me. :) I wish I could upvote this twice! –  BruceHill Jan 14 '14 at 14:23

I had a similar problem. I created the tab bar in storyboard and added all the images through the interface builder menus, none in code.

My fix was actually simple: under the attributes inspector window in IB, the Tab Bar Item field for "Selected Image" should be blank, and the Bar Item field for "Image" should be filled with the image you want.

I am running Xcode 6.0.1 and testing with iOS 8.0.2 devices.

share improve this answer
 

I had the same problem with Xcode 6.0.1 (6A317), seems to be a bug with Interface builder.However, i've managed to solve it by leaving selected image empty in interface builder then at each viewDidLoad in my view controllers i did insert:

[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];

it works well now, showing my selectedImage and with global tint mask.

share improve this answer
 

Based on the answer from 132206, I made this method for AppDelegate, called from application:didFinishLaunchingWithOptions: with:

[self configureSelectedImages];

It obviously requires a strict naming convention for your tab images, but can also be re-used without editing. To state the obvious - name your selected tab bar images TabbarXXXSelected, where XXX equals the title of the tab bar item.

- (void)configureSelectedImages
{
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;

    for (UITabBarItem *tabBarItem in [tabBar items]) {
        NSString *selectedImage = [NSString stringWithFormat:@"Tabbar%@Selected", [tabBarItem title]];
        (void)[tabBarItem initWithTitle:[tabBarItem title] image:[tabBarItem image] selectedImage:[UIImage imageNamed:selectedImage]];
    }
}
share improve this answer
 

You should write to the function:

UIImage* tab_image = [UIImage imageNamed:@"tab_image.png"];
UIImage* tab_image_selected = [UIImage imageNamed:@"tab_image_selected.png"];

tab_image = [tab_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab_image_selected = [tab_image_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.tabBarItem.image = tab_image;
self.tabBarItem.selectedImage = tab_image_selected;

I hope this helps

share improve this answer
 
 
Just in case you didn't know, you can use markdown to format code snippets in the body of your post. This helps make your question/answer more concise and readable. Additionally, make sure you're actually answering the original poster's question. –  remus Dec 12 '13 at 16:44

In your first view controller's .h file, I added the following: @property (weak, nonatomic) IBOutlet UITabBarItem *mapViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *profileViewTabBarItem; @property (weak, nonatomic) IBOutlet UITabBarItem *notificationViewTabBarItem;

(note that the mapViewTabBarItem was linked by ctrl dragging the actual tab bar item into the list of property declarations at the top of the .h file)

Next, in the same view controller's .m file in the viewDidLoad, add the following:

self.tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
_mapViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-map-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
self.tabBarItem.image = [[UIImage imageNamed:@"@2x-map-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

_profileViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:1];
_profileViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-profile-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_profileViewTabBarItem.image = [[UIImage imageNamed:@"@2x-profile-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

_notificationViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:2];
_notificationViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-notifications-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_notificationViewTabBarItem.image = [[UIImage imageNamed:@"@2x-notifications-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
share improve this answer
 

Try this:

UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-Small-50.png"] tag:100];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"image-50.png"] tag:200];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-clip-50.png"] tag:300];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-color-50.png"] tag:400];
UITabBarItem *item5 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-lock-50.png"] tag:500];

[item1 setSelectedImage:[[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item2 setSelectedImage:[[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item2 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item3 setSelectedImage:[[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item4 setSelectedImage:[[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item5 setSelectedImage:[[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item5 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

item1.image = [[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item2.image = [[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item3.image = [[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4.image = [[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item5.image = [[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
share improve this answer
 
 
Could you please elaborate more your answer adding a little more description about the solution you provide? –  abarisone May 7 at 9:45

I had the same issue. But working with StoryBoards prevented me from changing anything in the code. Leaving the image empty in the storyboard removed this obstacle for me. However putting the initWithTitle in the viewWillAppear method of the tab's viewcontroller gave me odd behaviour. First getting the selected image required an additional click and images did not show up for the non-initial tabs.

For me fixing this was adding the following code to the AppDelegate in the DidFinishLoadingWithOptions (similar to 132206 and Amitabha):

NSArray * vcs = [(UITabBarController*)self.window.rootViewController viewControllers];
UIViewController *tab0 = [[(UINavigationController*)[vcs objectAtIndex:0] viewControllers] objectAtIndex:0];
tab0.title = NSLocalizedString(@"Time", nil);
tab0.tabBarItem =  [[UITabBarItem alloc] initWithTitle:tab0.title image:[UIImage imageNamed:@"Recents.png"] selectedImage:[UIImage imageNamed:@"RecentsSolid.png"]];
UIViewController *tab1 = [[(UINavigationController*)[vcs objectAtIndex:1] viewControllers] objectAtIndex:0];
tab1.title = NSLocalizedString(@"Expense", nil);
tab1.tabBarItem = [[UITabBarItem alloc] initWithTitle:tab1.title image:[UIImage imageNamed:@"Euro.png"] selectedImage:[UIImage imageNamed:@"EuroSolid.png"]];
share improve this answer
 

Use the code below to fix the image issue in iOS7:

[[UITabBarItem alloc] initWithTitle:@"title" image:[[UIImage imageNamed:@"YOUR_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"YOUR_SEL_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
share improve this answer
 

It is easy and clean solution of category for UITabBar Items.

Just create category and use the Runtime Attribute and refer it from category like below.Add Runtime Attribute for selected TabBarItem Refer it from category and make changes

#import "UITabBarItem+CustomTabBar.h"

@implementation UITabBarItem (CustomTabBar)

-(void)setValue:(id)value forKey:(NSString *)key {
    if([key isEqualToString:@"tabtitle"]){
        if([value isEqualToString:@"contacts"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"contacts-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        } else if([value isEqualToString:@"chat"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"chat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        } else if([value isEqualToString:@"groupchat"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"groupchat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        } else if([value isEqualToString:@"settings"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"settings-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        }
    }
    [self setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Roboto-Regular" size:12.0f], NSFontAttributeName, [UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}

@end
share improve this answer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值