您的位置:首页 > 产品设计 > UI/UE

UITabBarItem中图片和文字的设置

2016-09-23 10:22 423 查看
1、UITabBarItem中的图片在选中时,默认会被渲染为蓝色,怎样避免渲染?

(1)通过在 Images.xcassets 中选择对应的图片,然后按照下图,选择“Original Image“,即可避免该图片被渲染。



(2)通过代码来实现(推荐使用第一种方法),因为如果该图片被使用多次时,要多次通过代码来设置,而第一种方法只需要设置一次即可。

UIImage *sltImage = [UIImage imageNamed:@"tabBar_icon"];
sltImage = [sltImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.selectedImage = sltImage;


2、设置UITabBarItem中文字的字体大小和颜色

// 通过appearance统一设置所有UITabBarItem的文字属性
// 后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置
// setTitleTextAttributes: forState: 方法后就带有UI_APPEARANCE_SELECTOR

NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];

NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];

UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐