普通状态下的文字属性
// 普通状态下的文字属性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionaryWithCapacity:0];
// 设置字体
normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
// 设置文字颜色
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// 设置
[vc.tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
选中状态下的文字属性
// 选中状态下的文字属性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionaryWithCapacity:0];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
// 设置
[vc.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
添加子控制器
/**
* 添加所有的子控制器
*/
- (void)setupChildVcs
{
[self setupChildVc:[[XMGEssenceViewController alloc] init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
[self setupChildVc:[[XMGNewViewController alloc] init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
[self setupChildVc:[[XMGFriendTrendsViewController alloc] init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
[self setupChildVc:[[XMGMeViewController alloc] initWithStyle:UITableViewStyleGrouped] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
}
/**
* 添加一个子控制器
* @param title 文字
* @param image 图片
* @param selectedImage 选中时的图片
*/
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
// 包装一个导航控制器
XMGNavigationController *nav = [[XMGNavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
// 设置子控制器的tabBarItem
nav.tabBarItem.title = title;
nav.tabBarItem.image = [UIImage imageNamed:image];
nav.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
}
/**
* 设置item属性
*/
- (void)setupItem
{
// UIControlStateNormal状态下的文字属性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
// 文字颜色
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// 文字大小
normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
// UIControlStateSelected状态下的文字属性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
// 文字颜色
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
// 统一给所有的UITabBarItem设置文字属性
// 只有后面带有UI_APPEARANCE_SELECTOR的属性或方法, 才可以通过appearance对象来统一设置
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
自定义tabbaritem
#import "TTTabBar.h"
#import "TTPublishViewController.h"
@interface XMGTabBar()
/** 发布按钮 */
@property (nonatomic, weak) UIButton *publishButton;
@end
@implementation XMGTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 设置背景图片
self.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
// 添加发布按钮
UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[publishButton sizeToFit];
[publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:publishButton];
self.publishButton = publishButton;
}
return self;
}
- (void)publishClick
{
TTPublishViewController *publish = [[TTPublishViewController alloc] init];
[self.window.rootViewController presentViewController:publish animated:NO completion:nil];
}
/**
* 布局子控件
*/
- (void)layoutSubviews
{
[super layoutSubviews];
// tabBar的尺寸
CGFloat width = self.width;
CGFloat height = self.height;
// 设置发布按钮的位置
self.publishButton.center = CGPointMake(width * 0.5, height * 0.5);
// 按钮索引
int index = 0;
// 按钮的尺寸
CGFloat tabBarButtonW = width / 5;
CGFloat tabBarButtonH = height;
CGFloat tabBarButtonY = 0;
// 设置4个TabBarButton的frame
for (UIView *tabBarButton in self.subviews) {
if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) continue;
// 计算按钮的X值
CGFloat tabBarButtonX = index * tabBarButtonW;
if (index >= 2) { // 给后面2个button增加一个宽度的X值
tabBarButtonX += tabBarButtonW;
}
// 设置按钮的frame
tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);
// 增加索引
index++;
}
}
@end
在自定义的TabBarController中
/**
* 处理TabBar
*/
- (void)setupTabBar
{
// 因为tabBar是只读属性,通过KVC进行设置
[self setValue:[[TTTabBar alloc] init] forKeyPath:@"tabBar"];
}