标签:
一.项目整体框架搭建
二.UI主框架结构及知识点
1>.代码封装思想
封装前的代码:(四个标题需要重复写四次,重复代码较多)
HomeViewController *HomeVC = [[HomeViewController alloc] init];
UINavigationController *HomeNV = [[UINavigationController alloc] initWithRootViewController:HomeVC];
//tabBarItem标题文字设置
HomeVC.tabBarItem.title = @"首页";
HomeVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];
//这种设置图片的方法可以保持原有颜色
HomeVC.tabBarItem.selectedImage= [[UIImage imageNamed:@"tabbar_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self addChildViewController:HomeNV];
//设置选中状态下的图片颜色--------------------
NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[HomeVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
//设置正常情况下的文字颜色
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
[HomeVC.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
封装后的代码:(将重复部分封装起来,使用时候调用即可) 同时这里面对与相中状态下图片的颜色及如何保持原色的方法需要掌握
-(void)loadTabBarVC
{
HomeViewController *homeVC = [[HomeViewController alloc]init];
[self addChildVC:homeVC title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
MessageViewController *MessageVC = [[MessageViewController alloc]init];
[self addChildVC:MessageVC title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"]; //调用方法
DiscoverViewController *DiscoverVC = [[DiscoverViewController alloc]init];
[self addChildVC:DiscoverVC title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
ProflieViewController *ProflieVC = [[ProflieViewController alloc]init];
[self addChildVC:ProflieVC title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
}
/*
添加一个子控制器
1.子控制器 childVC
2.标题 title
3.图片 image
4.选中的图片 selectedImage
*/
-(void)addChildVC:(UIViewController *)childVC title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
childVC.title = title; //同时设置tabBar和navigationBar的文字
// childVC.tabBarItem.title = title; //设置tabBar的文字
// childVC.navigationItem.title = title; //设置navigationBar的文字
//设置子控制器的图片
childVC.tabBarItem.image = [UIImage imageNamed:image];
childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//设置文字的样式
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = KColor(123, 123, 123);
NSMutableDictionary *selectTextAtrs = [NSMutableDictionary dictionary];
selectTextAtrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[childVC.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[childVC.tabBarItem setTitleTextAttributes:selectTextAtrs forState:UIControlStateSelected];
childVC.view.backgroundColor = KRandomColor;
//先给外面传进来的先控制器 包装 一个导航控制器
NavigationController *navigationNV = [[NavigationController alloc] initWithRootViewController:childVC];
//添加为子控制器
[self addChildViewController:navigationNV];
}
标签:
原文地址:http://www.cnblogs.com/erdeng/p/4890507.html