StoryBoard 设置 item 的图片和 颜色

1.设置seleectedImage和image


设置图片.png

2.这时候,会发现,不管你怎么弄,只会显示颜色不会正常显示图片,那是因为没有设置图片的renderingMode属性:

UIImageRenderingModeAutomatic  // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。  
UIImageRenderingModeAlwaysOriginal   // 始终绘制图片原始状态,不使用Tint Color。  
UIImageRenderingModeAlwaysTemplate   // 始终根据Tint Color绘制图片,忽略图片的颜色信息。 

设置选中图片和未选中图片的renderingMode属性为:

for (UITabBarItem *item in self.tabBar.items) {
        item.selectedImage = [item.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //item.title
    }

这样就可以正常显示了!

  1. 改变文字的颜色:先点击下图所选位置
选中.png

然后在下图出修改 ,bar tint是设置这个tabbar的背景色,下面的tint就可以设置所想要字体的颜色了


设置颜色.png

补充:代码设置tabbar item的文字颜色?

初始化视图控件
- (void)initView {
    NSMutableArray *imageArray = [[NSMutableArray alloc] initWithObjects:Imageone, ImageAccounttwo, ImageSecuritythree ImagePromotionfour, nil];
  
    self.selectedIndex = 2;
    
    for (UITabBarItem *item in self.tabBar.items) {
        // 设置 TabBar Item 被选中图标的模式
        item.selectedImage = [[UIImage imageNamed:[imageArray firstObject]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [imageArray removeObjectAtIndex:0];
        
        // 设置 TabBar Item 被选中图标的字体颜色
        if (self.selectedIndex == [self.tabBar.items indexOfObject:item]) {
            
            [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x549ACC), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        } else {
            [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x999999), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        }
    }
}

补充 : 当点击某个item的时候,会调用代理方法,然后再更改tabbar item 的字体颜色

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    // 设置 TabBar Item 默认的字体颜色
    for (UITabBarItem *item in self.tabBar.items) {
        [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x999999), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    }
    // 设置 TabBar 被选中 Item 的字体颜色
    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithHex(0x549ACC), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容