IOS笔记049-UITabBarController
1、简单实现
效果:在视图底部显示一个工具栏
代码实现
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 指定跟控制器
UITabBarController *tabBar = [[UITabBarController alloc] init];
self.window.rootViewController = tabBar;
UIViewController *vc1 = [[UIViewControlleralloc] init];
[tabBar addChildViewController:vc1];
vc1.view.backgroundColor = [UIColorredColor];
vc1.tabBarItem.title = @"聊天";
vc1.tabBarItem.image = [UIImage imageNamed:@“tab_recent_nor"];
UIViewController *vc2 = [[UIViewControlleralloc] init];
[tabBar addChildViewController:vc2];
vc2.view.backgroundColor = [UIColorgreenColor];
vc2.tabBarItem.title = @"空间";
vc2.tabBarItem.image = [UIImage imageNamed:@“tab_qworld_nor"];
UIViewController *vc3 = [[UIViewControlleralloc] init];
[tabBar addChildViewController:vc3];
vc3.view.backgroundColor = [UIColorgreenColor];
vc3.tabBarItem.title = @"联系人";
vc3.tabBarItem.image = [UIImage imageNamed:@“tab_buddy_nor"];
UIViewController *vc4 = [[UIViewControlleralloc] init];
[tabBar addChildViewController:vc4];
vc4.view.backgroundColor = [UIColorgreenColor];
vc4.tabBarItem.title = @"设置";
vc4.tabBarItem.image = [UIImage imageNamed:@“tab_me_nor"];
vc4.tabBarItem.badgeValue = @“10”; // 右上角显示数字
// 显示窗口
[self.windowmakeKeyAndVisible];
综合使用
UITabBarController的使用
一般是一个UITabBarController控制多个NavigationController,这样可以保证每一个控制器下都有一个单独的顶部导航栏,而不是整个应用只有一个导航栏。
比如下面这个界面,一个UITabBarController控制多个NavigationController,然后有NavigationController控制界面内部的跳转。
类似效果如下:
主界面有三个TableView组成,通过tabBarController进行控制。
在聊天界面选中cell后跳转到下一个控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *story = [UIStoryboardstoryboardWithName:@"Main"bundle:nil] ;
SLQChatViewController *chat = [story instantiateViewControllerWithIdentifier:@"chat"];
// 选中cell后跳转到chat界面
[self.navigationControllerpushViewController:chat animated:YES];
}
在设置界面的关于条目通过拖线建立与关于控制器的跳转。
注意情况
主要是各种cell的自定义方法。因为很多界面都是自定义的cell组成的。
还有就是从storyboard中加载控制器进行界面的跳转
UIStoryboard *story = [UIStoryboard storyboardWithName:@“Main"bundle:nil] ;
SLQChatViewController *chat = [story instantiateViewControllerWithIdentifier:@"chat"];
// 选中cell后跳转到chat界面
[self.navigationController pushViewController:chat animated:YES];
隐藏底部导航条可以设置控制的属性
本文来自博客园,作者:struggle_time,转载请注明原文链接:https://www.cnblogs.com/songliquan/p/4583211.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?