一.项目整体框架搭建
二.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];
}
2.技术细节
1>.如何在push到下一页tabbarController的tabBar让自动隐藏
//在底部有tabbarController的情况下,通过这种方法在推出下一页的情况下会让tabBar会自动隐藏
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Test1ViewController *text1 = [[Test1ViewController alloc] init];
//当test1控制器被push的时候, test1所在的tabbarController的tabBar会自动隐藏
//当test1控制器被pop的时候,test1所在的tabbarController的tabBar会自动显示
text1.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:text1 animated:YES];
}
2>.统一所有控制器导航栏左上角和右上角的内容
(1).让所有push进来的控制器,它导航栏左上角和右上角的内容都一样
(2)."拦截"所有push进来的控制器
(3).方案:自定义导航控制器,重写push方法,就可以得到传进来的控制器参数
// 90%的"拦截"都是通过自定义类,重写自带的方法实现的
实例代码:(通过自定义navigationController中重写push方法,可以同一设置控制器导航栏左上角和右上角的内容,同时每个界面的导航栏又可以自己进行修改)
//在自定义navigationController中重写push方法
//重写这个方法目的: 能够拦截所有的push进来的控制器
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//这个时候push进来的控制器不是第一个控制器
if (self.viewControllers.count > 0) {
/**自动显示和隐藏tabBar */
viewController.hidesBottomBarWhenPushed = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, button.currentBackgroundImage.size.width, button.currentBackgroundImage.size.height);
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button ];
UIButton *buttonmore = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonmore addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside];
[buttonmore setBackgroundImage:[UIImage imageNamed:@"navigationbar_more"] forState:UIControlStateNormal];
[buttonmore setBackgroundImage:[UIImage imageNamed:@"navigationbar_more_highlighted"] forState:UIControlStateHighlighted];
buttonmore.frame = CGRectMake(0, 0, buttonmore.currentBackgroundImage.size.width, buttonmore.currentBackgroundImage.size.height);
viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonmore ];
}
//需要实现父类的push方法
[super pushViewController:viewController animated:YES];
NSLog(@"%@",viewController);
}
-(void)back
{
#warning 这里要用self 不是self.navigationController 因为self.navigationController为空
NSLog(@"%@",self.navigationController);
//self本身为navigationtionController
[self popViewControllerAnimated:YES];
}
-(void)more
{
[self popToRootViewControllerAnimated:YES];
}
3>.常见错误"duplicate symbol _OBJC_METACLASS_$_类名 in:"错误
(1).90%都是因为#import了.m文件 (2).其他可能是因为项目中存在了2个一样的.m文件