iOS- 06 项目构架-tabBar坐底-Nav做顶的封装

  • 代码实现
    #import "ZYTabBarViewController.h"

   #import "ZYEssenceViewController.h"
   #import "ZYFriendTrendsViewController.h"
   #import "ZYMeViewController.h"
   #import "ZYNewViewController.h"
   #import "ZYPublishViewController.h"
   #import "UIImage+Image.h"
   @interface ZYTabBarViewController()
   @end
   @implementation ZYTabBarViewController
   /**利用富文本设置tabbar的标题颜色和尺寸*/
   + (void)load
  {
   //  获取哪个类中UITabBarItem
     UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    //  设置按钮选中标题的颜色
     NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
     attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
     [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
     //   设置字体尺寸--只能设置正常状态下
     NSMutableDictionary *attrsNor = [[NSMutableDictionary alloc]init];
     attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
     [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
    }
     /**封装添加子控制器*/
   - (void)setUpOneChildViewController:(UIViewController *)vc image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title isHasNav:(BOOL)isHasNav
    {
       vc.tabBarItem.image = [UIImage imageNamed:image];
       vc.tabBarItem.selectedImage = [UIImage imageOriginalWith:selectedImage];
       if (isHasNav == YES) {
    vc.title = title;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:nav];
   }else{
     [self addChildViewController:vc];
    }
    }
   #pragma mark- 添加所有子控制器
  - (void)setupAllChildViewController
  {  //  精华
     ZYEssenceViewController *easence = [[ZYEssenceViewController alloc]init];
     [self setUpOneChildViewController:easence image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon" title:@"精华" isHasNav:YES];
     //  新帖
     ZYNewViewController *new = [[ZYNewViewController alloc]init];
     [self setUpOneChildViewController:new image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_new_click_icon" title:@"新帖" isHasNav:YES];
     //  发布
     ZYPublishViewController *publish = [[ZYPublishViewController alloc]init];
     [self setUpOneChildViewController:publish image:@"tabBar_publish_icon" selectedImage:@"tabBar_publish_click_icon" title:nil isHasNav:NO];
     //  关注
     ZYFriendTrendsViewController *friendTrends = [[ZYFriendTrendsViewController alloc]init];
     [self setUpOneChildViewController:friendTrends image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon" title:@"关注" isHasNav:YES];
     //   我的
     ZYMeViewController *me = [[ZYMeViewController alloc]init];
     [self setUpOneChildViewController:me image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon" title:@"我的" isHasNav:YES];
  }
    #pragma mark - 生命周期
  - (void)viewDidLoad
   {  [super viewDidLoad];
   //  设置子控制器并添加按钮的内容
   [self setupAllChildViewController];
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 开发是一个学习的过程,当你在项目中遇到难点的时候,第一个想到的应该是Google,百度...我总是拿这样一句话来形...
    Senior丶阅读 4,836评论 35 67
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,554评论 1 14
  • 简介 UITabBar是iOS App中经常使用的系统控件,比如知名App:新浪微博,微信,腾讯QQ等。在实际的项...
    清蘂翅膀的技术阅读 1,901评论 0 6
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,458评论 9 5
  • 从前有两只牛蛙,性别男,爱好♂。 有传说,在七夕的夜晚,若一只牛蛙能够成功获得一位人类女孩的亲吻,他就可以变为一个...
    忆无心阅读 791评论 2 1