教你十分钟搭建一个很好的tabbar模式的项目(纯代码)

先来看一下效果图:

1.支持导航栏颜色自定义
2.支持返回按钮自定义
3.支持导航栏右侧按钮自定义
4.支持导航栏标题自定义
5.支持所有页面手势侧滑
6.支持tabbar item 文字颜色自定义(选中状态和非选中状态)


  • 1.导入原资源,如tabbar的items图片等
  • 2.创建.pch文件

在项目->Bulid Settings->搜prefix header->在后面加入你的.pch文件的绝对路径
(一个简单方法就是点出prefix header后面的框后,把工程目录里面的.pch文件拖到框里,就自动生成它的绝对路径了)

  • 3.导入需要使用的三方库MLNavigationController,这个控制器继承自UINavigationController,加入了返回手势也可以用系统的导航栏,系统的导航栏现在也自带返回滑动手势
  • 4.创建继承于UITabBarController的tabbar控制器
  • 5.AppDelegate中初始化
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];           
self.window.rootViewController = [[SJTabBarViewController alloc] init];;
[self.window makeKeyAndVisible];
  • 6.创建基类控制器 如:SJBaseViewController。在基类控制器里面主要设置导航栏及其一些属性
    ~6.1 基类 .h文件



    ~6.2 基类 .m文件
    //设置导航栏颜色

 - (void)setNavBarStyle
{
        self.titleViewLB.textColor = color_whiteColor;
        self.navigationController.navigationBar.barTintColor = kNavigationBarColor;
        self.navigationController.navigationBar.translucent = NO;
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        UINavigationBar *navigationBar = self.navigationController.navigationBar;
        [navigationBar setBackgroundImage:[UIImage imageNamed:@"account_headBg"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        [navigationBar setShadowImage:[UIImage new]];
}

//baseBack方法

 - (void)baseBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

//setter and getter方法

 - (UILabel *)titleViewLB
{
    if (_titleViewLB == nil) {
     _titleViewLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth-140, 44)];
     _titleViewLB.backgroundColor = [UIColor clearColor];
     _titleViewLB.textColor = color_whiteColor
     _titleViewLB.font = XSBlodFont(19);
     _titleViewLB.textAlignment = NSTextAlignmentCenter;
    }
    return _titleViewLB;
}

 - (void)setNavTitle:(NSString *)navTitle{
    _titleViewLB.text = navTitle;
}

 - (void)setNavTitleColor:(UIColor *)navTitleColor{
    _titleViewLB.textColor = navTitleColor;
}

 - (void)setShowBack:(BOOL)showBack
{
    if (showBack) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backButton.frame = CGRectMake(0, 0, 20, 44);
        [_backButton setImage:[UIImage imageNamed:@"back-white"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(baseBack) forControlEvents:UIControlEventTouchUpInside];
        _backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:_backButton];
        backItem.style = UIBarButtonItemStylePlain;
        self.navigationItem.leftBarButtonItem = backItem;
    }else{
        self.navigationItem.hidesBackButton = YES;
    }
    //解决titleView不能居中的问题
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 35, 35);
    button.hidden = YES;
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = right;
}

 - (void)setShowLeftButton:(BOOL)showLeftButton
{
    if (showLeftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 35, 35);
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        [button addTarget:self action:@selector(leftBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside];
    }
}

 - (void)setShowRightButton:(BOOL)showRightButton
{
    if (showRightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(0, 0, 30, 30);
        _rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
        //可以根据自己想要的图标在目标控制器里设置
//        [_rightButton setImage:[UIImage imageNamed:@"more_icon_forum"] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
        [_rightButton addTarget:self action:@selector(rightBarButtonItemTouchedUpInSide) forControlEvents:UIControlEventTouchUpInside];
    }
}

//touch up inside响应按钮方法

 - (void)leftBarButtonItemTouchedUpInSide
{
    //根据自己需要实现
    NSLog(@"leftBarButtonItemTouchedUpInSide未实现");
}

 - (void)rightBarButtonItemTouchedUpInSide
{
    //根据自己需要实现
    NSLog(@"rightBarButtonItemTouchedUpInSide未实现");
}
  • 7.创建tabbar Items 控制器,tabbar有几个items就创建几个,继承自BaseViewController

  • 8.创建tabbar控制器,继承自UITabBarController
    ~8.1 遵守协议:UITabBarDelegate,UITabBarControllerDelegate
    ~8.2 items控制器组

    @property(nonatomic,strong)NSMutableArray *viewControllers;

~8.3 初始化控制器

 - (void)setUpControllers
{
    SJOneViewController * oneVC = [[SJOneViewController alloc] init];
    MLNavigationController *oneNav = [[MLNavigationController alloc] initWithRootViewController:oneVC];
    SJTwoViewController * twoVC = [[SJTwoViewController alloc] init];
    MLNavigationController *twoNav = [[MLNavigationController alloc] initWithRootViewController:twoVC];
    SJThreeViewController *threeVC = [[SJThreeViewController alloc] init];
    MLNavigationController *threeNav = [[MLNavigationController alloc] initWithRootViewController:threeVC];
    [self.viewControllers addObject:oneNav];
    [self.viewControllers addObject:twoNav];
    [self.viewControllers addObject:threeNav];
}

~8.4 添加子控制器,初始化tabbarItem 方法

 - (void)addController:(UIViewController *)viewController title:(NSString *)title normolImageName:(NSString *)normalImageName selectImageName:(NSString *)selectImageName
{
    viewController.tabBarItem.title = title;
    
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.image = normalImage;
    
    UIImage *selectedImage = [UIImage imageNamed:selectImageName];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = selectedImage;
    
    [self addChildViewController:viewController];
}

~8.5 设置文字不同状态下属性方法

 - (void)setUpTabbarItemTextAttributes
{
    //普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.00f green:0.64f blue:0.51f alpha:1.00f];
    //选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    UITabBarItem *tabbarItem = [UITabBarItem appearance];
    [tabbarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabbarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

~8.6 UITabBarControllerDelegate代理方法(可选实现)

 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([viewController isKindOfClass:[UINavigationController class]]) {
     UINavigationController *nav = (UINavigationController *)viewController;
     UIViewController *vc = [nav.viewControllers objectAtIndex:0];
     NSLog(@"current viewcontroller is %@", vc);
    }
    return YES;
}

~8.7 tabbar items 各属性赋值(核心代码示例)

NSArray *array = @[@{@"title":@"杀敌",@"normalImageName":@"battle_win_kill",@"selectImageName":@"battle_lose_kill"},
                       @{@"title":@"死亡",@"normalImageName":@"battle_win_death",@"selectImageName":@"battle_lose_death"},
                       @{@"title":@"助攻",@"normalImageName":@"battle_win_assist",@"selectImageName":@"battle_lose_assist"}];
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dic = (NSDictionary *)obj;
        UIViewController *vc = self.viewControllers[idx];
        NSString *title = [dic objectForKey:@"title"];
        NSString *normalImageName = [dic objectForKey:@"normalImageName"];
        NSString *selectImageName = [dic objectForKey:@"selectImageName"];
        
        [self addController:vc title:title normolImageName:normalImageName selectImageName:selectImageName];
    }];
    [self setUpTabbarItemTextAttributes];
    self.delegate = self;
  • 9.至此基本完毕,在目标控制器里面,可以选择性的实现基类的一些方法来自定义目标控制器,如:
    ①目标控制器的navTitle
    <pre>[self setNavTitle:@"杀敌"];</pre>
    ②是否有返回按钮,默认无
    <pre>[self setShowRightButton:YES];</pre>
    ③设置navigationItem.rightBarButtonItem的图标
    <pre>[self.rightButton setImage:[UIImage imageNamed:@"battle_win_death"] forState:UIControlStateNormal];</pre>
    ④rightBarButtonItem的点击响应放法
    <pre>- (void)rightBarButtonItemTouchedUpInSide{
    //your code
    }</pre>
文章为作者辛苦码出来的,转载请注明出处,喜欢的话请star😍一下。示例代码链接:https://github.com/SPIREJ/SJTabbarProject
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,460评论 1 14
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,519评论 2 4
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 784评论 0 0
  • 赤裸裸的被人称赞,那种美妙的感觉久远的快忆不起来了.还好,昨天一个天籁般的声音,让我重拾美妙,瞬间激动的一塌糊涂....
    银子姐阅读 319评论 11 5
  • 一、CoreAnimation的介绍 Core Animation是一组非常强大的动画处理API,使用它能做出非常...
    任性不认命ToT阅读 338评论 0 0