您的位置:首页 > 移动开发 > IOS开发

iOS开发之初:自定义tabBar与tabBarController

2016-07-29 17:08 399 查看
不再多述,自定义出来的好处就是一次配置,终生受用..而且方便修理..不然代码都挤在一个控制器里自己想翻也翻得麻烦..

创建一个函数方法,用来添加子控制器到工具栏中,并对每个控制器的标题,图片进行配置..添加几个工具栏就有几个可选择的控制器

//这里添加的子控制器要装在自定义的NavigationController里:

[self setupOneChildViewController:[[XMGNavigationController alloc] initWithRootViewController:[[XMGEssenceViewController alloc] init]] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];


- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
vc.tabBarItem.title = title;
if (image.length) { // 图片名有具体值
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
}
[self addChildViewController:vc];
}


tabBarItem里的文字颜色大小一般也是有需要要改的.用这个方法

/**
*  设置所有UITabBarItem的文字属性
*/
- (void)setupItemTitleTextAttributes
{
UITabBarItem *item = [UITabBarItem appearance];
// 普通状态下的文字属性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
[item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
// 选中状态下的文字属性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
[item setTitleTextAttributes:normalAttrs forState:UIControlStateSelected];
}


在自定义的tabBarController的[viewDidLoad]里一般放这么三个方法

/**** 设置所有UITabBarItem的文字属性 ****/
[self setupItemTitleTextAttributes];

/**** 添加子控制器 ****/
[self setupChildViewControllers];

/**** 更换TabBar ****/
[self setupTabBar];


在tabBarController里面更换tabBar这个View的代码如下:

/**
*  更换TabBar
*/
- (void)setupTabBar
{
[self setValue:[[ZZFTabBar alloc] init] forKeyPath:@"tabBar"];
}


//如果tabBar没有特殊的按钮(类似发布/直播录制)和什么鬼东西,在满足需求的情况下一般也不需要自定义tabBar的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tabBarCont