这个基础框架主要包括UITabBarController、UINavigationController和UIBarButtonItem类的扩展。主要解决创建子视图过多的问题,将创建子视图用UINavigationController封装起来,然后添加到UITabBarController的ChildViewController视图中。为UITabBarController的UITabBarItem设置字体大小和颜色。话不多说,我们上代码。1.继承UITabBarController创建的NPTabBarController1.设置tabbar上的字体样式#pragmamark-设置tabbar上的字setTitleTextAttributes-(void)setTabBarTitleAttributesStyle{NSMutableDictionary*attrs=[NSMutableDictionarydictionary];//通常是字体大小attrs[NSFontAttributeName]=[UIFontsystemFontOfSize:16];//普通字体颜色attrs[NSForegroundColorAttributeName]=[UIColorlightGrayColor];NSMutableDictionary*selectAttrs=[NSMutableDictionarydictionary];//选择字体大小selectAttrs[NSFontAttributeName]=[UIFontsystemFontOfSize:16];//选择字体颜色selectAttrs[NSForegroundColor]]=[UIColordarkGrayColor];//UI_APPEARANCE_SELECTOR外观UITabBarItem*item=[UITabBarItemappearance];//Tabbar普通字体样式[itemsetTitleTextAttributes:attrsforState:UIControlStateNormal];//Tabbar选中字体样式[itemsetTitleTextAttributes:selectAttrsforState:UIControlState}2Navigation];Subviewencapsulation#pragmamark-navigationsubviewencapsulation-(void)setChildVC:(UIViewController*)ChildVCtitle:(NSString*)titleimage:(NSString*)imageselectImgage:(NSString*)selectImage{//注意视图层级,当它是在顶部UINavigationController,创建view就是给一个UINavigationControllerNPNavigationController*nav=[[NPNavigationControlleralloc]initWithRootViewController:ChildVC];//子view显示在UITabbarController上显示的tabbar标题nav.tabBarItem.title=title;//子view显示在UITabbarController上TheUITabbarControllerwithoutTheimageoftheselectedtabbarnav.tabBarItem.image=[UIImageimageNamed:image];//子视图在UITabbarController上显示选中的tabbarnav.tabBarItem.selectedImage=[UIImageimageNamed:selectImage];//背景子视图nav.view的颜色。backgroundColor=[UIColorgrayColor];//注意标题显示的是子view的标题,不是UINavigationControllerChildVC.navigationItem.title=title;//添加子view[selfaddChildViewController:nav];}2.继承UINavigationController创建NPNavigationController1.重写-(void)pushVewController:(UIViewConntroller*)viewCOntrolleranimated:(Bool)animated方法-(void)pushViewController:(UIViewController*)viewControlleranimated:(BOOL)animated//判断self.childViewControllers是否有所有子控制器,self.childViewControllers当.count为0时,只有childcontroller,杀掉返回按钮if(self.childViewControllers.count>0){//自定义返回按钮UIButton*returnBtn=[UIButtonbuttonWithType:UIButtonTypeCustom];//设置标题:return[returnBtnsetTitle:@"return"forState:UIControlStateNormal];//普通图片[returnBtnsetImage:[UIImageimageNamed:@"black"]forState:UIControlStateNormal];//高亮图片[returnBtnsetImage:[UIImageimageNamed:@"grat"]forState:UIControlStateHighlighted];//普通标题颜色按钮位置,注意这个位置没有作用[returnBtnsetFrame:CGRectMake(0,0,70,20)];//按钮内部对齐[returnBtnsetContentHorizo??ntalAlignment:UIControlContentHorizo??ntalAlignmentLeft];//设置图片内部位置[returnBtnsetContentEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];//添加点击事件[returnBtnaddTarget:selfaction:@selector(returnBtnClick)forControlEvents:UIControlEventTouchUpInside];//隐藏tabbarviewController.hidesBottomBarWhenPushed=YES时theviewispushed;//自定义推送视图的leftBarButtonItemviewController。navigationItem.leftBarButtonItem=[[UIBarButtonItemalloc]initWithCustomView:returnBtn];}//子控制器通过该方法推送view[superpushViewController:viewControlleranimated:animated];3、添加UIBarButtonItem类扩展UIBarButtonItem+NPBarbutton,创建UIbarbuttonItem类方法1.UIbarbuttonItem类方法/***创建一个UIBarbuttonItem类方法,该方法是创建按钮的类方法,传入的参数:正常显示的图片image,hightImage高亮时显示的图片,target使用的对象,action的行为click*/+(instancetype)itemWithImage:(NSString*)imagehightImage:(NSString*)hightImagetarget:(id)targetaction:(SEL)action{UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];[btnsetBackgroundImage:[UIImageimageNamed:image]forState:UIControlStateNormal];[btnsetBackgroundImage:[UIImageimageNamed:hightImage]forState:UIControlStateNormal];//btnsize是北京图片的大小CGSizebtnsize=btn.currentBackgroundImage.size;//设置btn的边框[btnsetFrame:CGRectMake(0,0,btnsize.width,btnsize.height)];[btnaddTarget:targetaction:actionforControlEvents:UIControlEventTouchUpInside];return[[selfalloc]initWithCustomView:btn];}以上就是基本框架的主要实现,还有不足之处,手势滑动返回就不写了。MarkDown语法推广链接:http://www.jianshu.com/p/7cc9c26e8b7a作者:大猫集团本文Demo下载地址:http://code.cocoachina.com/view/129999作者:NiePlus
