【iOS开发-78】用代码实现UITabBarController+UINavigationController

本文介绍如何使用iOS中的TabBar控制器,并设置其标题、图标及背景颜色。通过addChildViewController方法添加四个不同功能的视图控制器,实现消息、联系人、动态和设置四个模块的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#import "WPTabBarController.h"
#import "WPFirstViewController.h"
#import "WPSecondViewController.h"
#import "WPThirdViewController.h"
#import "WPFourthViewController.h"

@interface WPTabBarController ()

@end

@implementation WPTabBarController

- (void)viewDidLoad {
    WPFirstViewController *firsVc=[[WPFirstViewController alloc]init];
    WPSecondViewController *secondVc=[[WPSecondViewController alloc]init];
    WPThirdViewController *thirdVc=[[WPThirdViewController alloc]init];
    WPFourthViewController *fourthVc=[[WPFourthViewController alloc]init];
    
    //以下两种方式均可设置tabbar上得标题
    firsVc.tabBarItem.title=@"消息";
    //firsVc.title=@"消息";
    secondVc.tabBarItem.title=@"联系人";
    //secondVc.title=@"联系人";
    thirdVc.tabBarItem.title=@"动态";
    //thirdVc.title=@"动态";
    fourthVc.tabBarItem.title=@"设置";
    //fourthVc.title=@"设置";

    //设置icon
    firsVc.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
    secondVc.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];
    thirdVc.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];
    fourthVc.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"];
    
    firsVc.view.backgroundColor=[UIColor redColor];
    secondVc.view.backgroundColor=[UIColor whiteColor];
    thirdVc.view.backgroundColor=[UIColor purpleColor];
    fourthVc.view.backgroundColor=[UIColor greenColor];
    
    
    //用以下方式添加子控制器
    //self.viewControllers=@[firsVc,secondVc,thirdVc,fourthVc];
    [self addChildViewController:firsVc];
    [self addChildViewController:secondVc];
    [self addChildViewController:thirdVc];
    [self addChildViewController:fourthVc];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

核心:

——定义1个tabBat控制器和4个导航控制器,然后通过addChildViewController或者数组的形式把4个导航控制器加到tabBar控制器中。

——每个导航控制器通过tabBarItem属性来设置标题和icon等。

界面截屏:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值