//**标签栏控制器的初始化
UITabBarController * tabbarC = [[UITabBarController alloc] init];
//设置tabBar的颜色
tabbarC.tabBar.barTintColor = [UIColor brownColor];
//设置按钮颜色
tabbarC.tabBar.tintColor = [UIColor cyanColor];
//**创建视图控制器,类似ViewController
UIViewController * c1 = [[UIViewController alloc] init];
c1.view.backgroundColor = [UIColor greenColor];
//**创建系统自带的UITabBarItem
UITabBarItem * c1Item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0];
//另外两种种初始化方法
UITabBarItem * c2Item = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"tab_3"] selectedImage:selectedImage2];
UITabBarItem * c3Item = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tab_0"] tag:0];
//设置右上角红色小气泡
c1Item.badgeValue = @"100";
//**创建好的item赋值给tabBarItem
c1.tabBarItem = c1Item;
//第一种方式添加子控制器
[tabbar addChildViewController:c1];
[tabbar addChildViewController:c2];
[tabbar addChildViewController:c3];
//**第二种方式添加子控制器
tabbarC.viewControllers = @[c1,c2,c3,c4,c5,c6];
//**设置主控制器
self.window.rootViewController = tabbarC;
//**更改显示先后顺序
[self.window makeKeyAndVisible];
//更改选中状态图片UIImageRenderingModeAlwaysOriginal 不做任何修改的图片
self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_0"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//当导航视图控制器压栈时,隐藏tabbar
vc.hidesBottomBarWhenPushed = YES;
UITabBarController * tabbarVC = [[UITabBarController alloc] init];
FirstViewController * firstVC = [[FirstViewController alloc] init];
firstVC.title = @"第一页";
UINavigationController * firstNav = [[UINavigationController alloc] initWithRootViewController:firstVC];
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.title = @"第二页";
UINavigationController * secondNav = [[UINavigationController alloc] initWithRootViewController:secondVC];
ThirdViewController * thirdVC = [[ThirdViewController alloc] init];
thirdVC.title = @"第三页";
UINavigationController * thirdNav = [[UINavigationController alloc] initWithRootViewController:thirdVC];
tabbarVC.viewControllers = @[firstNav,secondNav,thirdNav];
tabbarVC.viewControllers = @[firstVC,secondVC,thirdVC];
self.window.rootViewController = tabbarVC;
//以视图控制器的名字创建字符串数组
NSMutableArray * viewControllerNames = [NSMutableArray arrayWithArray:@[@"FirstViewController",@"SecondViewController",@"ThirdViewController"]];
//以视图控制器的标题创建字符串数组
NSArray * titleNames = @[@"第一页",@"第二页",@"第三页"];
//遍历viewControllerNames数组
for (NSInteger i = 0; i < viewControllerNames.count; i++) {
//获取数组中的每一项
NSString * vcName = viewControllerNames[i];//FirstViewController
//将字符串转换为Class类型,并通过Class初始化对象
UIViewController * vc = [[NSClassFromString(vcName) alloc] init];
//设置题目
vc.title = titleNames[i];
//创建导航控制器
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
//nav替换viewControllerNames数组中的字符串
[viewControllerNames replaceObjectAtIndex:i withObject:nav];
}
UITabBarController * tabbarVC = [[UITabBarController alloc] init];
tabbarVC.viewControllers = viewControllerNames;
self.window.rootViewController = tabbarVC;
//以视图控制器的名字创建字符串数组
NSMutableArray * viewControllerNames = [NSMutableArray arrayWithArray:@[@"FirstViewController",@"SecondViewController",@"ThirdViewController"]];
//以视图控制器的标题创建字符串数组
NSArray * titleNames = @[@"第一页",@"第二页",@"第三页"];
//遍历viewControllerNames数组
for (NSInteger i = 0; i < viewControllerNames.count; i++) {
//获取数组中的每一项
NSString * vcName = viewControllerNames[i];//FirstViewController
//将字符串转换为Class类型,并通过Class初始化对象
UIViewController * vc = [[NSClassFromString(vcName) alloc] init];
//设置题目
vc.title = titleNames[i];
//创建导航控制器
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
//nav替换viewControllerNames数组中的字符串
[viewControllerNames replaceObjectAtIndex:i withObject:nav];
}
self.viewControllers = viewControllerNames;
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步