iOS——UITabBarController

2023-05-16

一、UITabBarController简介

1.UITabBarController 对象可以保存多个 视图控制器 并进行切换,并且底部还会有一个  工具栏(UITabBar),工具栏里会有多个标签项(UITabBarItem)
2. 当向 UITabBarController 对象中加入一个 UIViewController 对象时,系统会在 工具栏 自动创建一个 UITabBarItem对象并将其与加入的 UIViewController 对象关联起来,即点击当前的 UITabBarItem 对象时,就会显示该 UITabBarItem 对象所关联的 UIViewController 对象的视图
3. UITabBarController 对象默认会显示第一个 UIViewController 的视图,此时,并不会加载其他 UIViewController 的视图,只要当需要显示某个 UIViewController 的视图时,才会对其加载,否则就不加载

二、UITabBarController 属性

1. 保存视图控制器的数组
1)保存 UIViewController 的数组

@property(nullable,nonatomic,copy)NSArray<__kindofUIViewController *> *viewControllers;


2)一次性设置保存 UIViewController 的数组,并设置动画效果

- (void)setViewControllers:(NSArray<__kindofUIViewController *> * __nullable)viewControllers animated:(BOOL)animated;


假如在此之前已经创建好了 6 个 UIViewController 对象,并且都是用了系统的 UITabBarItem 对象,因为本节的重点不在这,所以就省略了


代码如下

// 创建 UITabBarController 对象
UITabBarController * tabBarController = [[UITabBarController alloc] init];
没有动画显示

tabBarController.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC, sixthVC];

使用动画显示

[tabBarController setViewControllers:@[firstVC, secondVC, thirdVC, fourthVC, fifthVC, sixthVC] animated:YES];
虽然结果和上面的图一致,但是在显示 工具栏的上的 UITabBarItem 对象是有一个动画的

2. 选中的 UIViewController 

1)设置选中的 UITabBarItem 的索引

@property(nonatomic)NSUInteger selectedIndex;


2)设置选中的视图控制器

@property(nullable,nonatomic,assign)__kindofUIViewController *selectedViewController;


因为 UITabBarController 对象在启动时会默认显示第一个 UIViewController 对象,即索引为 0 的 UIViewController


设置启动时显示下表为 3 的 UIViewController,即第 4 个 UIViewController 对象

tabBarController.selectedIndex = 3;

tabBarController.selectedViewController = fourthVC;

如图,启动时就已经显示在第 4 个 UIViewController



3. ”更多“ 

当 UITabBarController 对象显示的 UIViewController 对象超过 5 个时,系统会自动创建一个 UINavigationController 对象,并将在原本显示第 5 个 UITabBarItem 的地方显示一个 “more” 的 UITabBarItem 对象,并且该 “more” 对应的 UIViewController 是一个 UINavigationController 对象,然后再把第 5 个以后的 UIViewController 都加入到这个 UINavigationController 对象的栈中
1)返回 “more” 对应的 UINavigationController 对象

@property(nonatomic,readonly)UINavigationController *moreNavigationController;


2)保存 “更多” 中的视图控制器对象

@property(nullable,nonatomic,copy)NSArray<__kindofUIViewController *> *customizableViewControllers;


6. 获取 标签栏 对象

就是 UITabBarController 对象下方的工具条,一个 UIViewController 对象在 tabBar 中对应一个 UITabBarItem 对象

@property(nonatomic,readonly)UITabBar *tabBar;


8. 代理对象

@property(nullable,nonatomic,weak)id<UITabBarControllerDelegate> delegate;


三、UITabBarControllerDelegate

1. 点击 UITabBarItem 时调用,控制哪些 UIViewController 能被点击
返回 YES : 能被点击;返回 NO 时,不能被点击(也就是说不能调用 2 的方法)
viewController : 发送该消息的接受者将 viewControllers 属性传递给 viewController

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

2. 当选中某一个 UIViewController 时调用该方法(即在 UITabBar 中选中某个 UITabBarItem)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;


3. 开始 编辑 之前时调用

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindofUIViewController *> *)viewControllers;


4. 结束 编辑 之前时调用

changed : 若是改变了视图控制器的顺序,则返回 YES;否则返回 NO

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindofUIViewController *> *)viewControllers changed:(BOOL)changed;


5. 已经结束编辑时调用

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindofUIViewController *> *)viewControllers changed:(BOOL)changed;


6. 监控屏幕切换;返回 屏幕切换的枚举

- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController;


7. 屏幕首选的方向

- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController;


四、分类 UIViewController (UITabBarControllerItem)

1. 当前 UIViewController 的 UITabBarItem,如果要设置当前 UIViewController 的 UITabBarItem 的标题等,就要先获取该 UIViewController 的 tabBarItem 对象

@property(null_resettable,nonatomic,strong)UITabBarItem *tabBarItem;


2. 获取当前 UIViewController 的 UITabBarController 对象,如果有多个 UIViewController 对象都在同一个 UITabBarController 中,那么返回的是同一个 UITabBarController 镀对象

@property(nullable,nonatomic,readonly,strong)UITabBarController *tabBarController;


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS——UITabBarController 的相关文章

  • 是否可以使用 Firebase 安排推送通知? [复制]

    这个问题在这里已经有答案了 我已经阅读了我能找到的所有文档 但仍然不知道这是否可行 如果我是用户 我可以安排特定时间的推送通知吗 Example 1 我是用户并打开应用程序 2 我允许通知并转到 pickerView 或其他任何内容 并设置
  • 在 Xcode 5 中重命名 iOS 项目[重复]

    这个问题在这里已经有答案了 我需要重命名一个 iOS 项目 有没有办法在不开始一个全新项目的情况下做到这一点 我发现的所有其他信息都与 Xcode 4 或旧版本相关 这些方法似乎使项目崩溃 我在尝试任何名称更改之前创建了一个快照 在 Xco
  • 如何使用AudioKit保存音频文件?

    我有音频文件 我给它做了一些效果 let pitchshifter AKPitchShifter self audioPlayer pitchshifter shift 10 AudioKit output pitchshifter 如果我
  • 在 iOS 上将 NSString 转换为 NSDate 的正确方法?

    我一直在使用此方法将常规 NSString 对象转换为 NSDate 但尝试向 Apple 提交更新 但遭到拒绝 在 iOS 中还有什么其他方法可以做到这一点 NSString date str 2011 08 12T12 20 00Z N
  • UIButton的高亮状态由什么控制事件开始和结束

    我正在创建类似钢琴的视图UIButton作为钢琴键 什么UIControlEvents当按钮获得和失去突出显示状态时 我应该监听以获得回调吗 我试图创建子类UIButton并添加属性观察者highlighted并且运行良好 然而 有时我需要
  • 推送动画,没有阴影和停电

    我有一个简单的iOS NavigationController基于应用程序 二UICollectionViews 相继 如果元素打开 第一个合集 被点击时 第二集 将被打开 非常简单 重要的提示 Both UICollectionViews
  • ios水平居中约束问题?

    I am having hard time in learning constraints auto layout in iOS I have used any width any height I have a storyboard sc
  • PFQueryTableViewController 错误

    我正在遵循在线教程 使用 Parse 作为后端创建照片共享应用程序 我已经运行了两次教程 两次都从头开始创建应用程序 但在同一位置仍然出现相同的错误 我到处寻找解决方案 但仍然没有运气 我正在使用 PFQueryTableViewContr
  • Swift 中的 import 语句是否有相关成本?

    阅读字符串宣言 我看到一个段落 https github com apple swift blob master docs StringManifesto md batteries included关于避免Foundation不需要的时候导
  • 无法在 ios 应用程序中通过 googlecast 正确投射视频

    我正在开发一个基于 AVPlayer 的自定义视频播放器项目 尝试整合谷歌演员 我已经根据谷歌图进行了集成 https codelabs developers google com codelabs cast videos ios http
  • 如何使用 CNContacts 快速获取手机号码?

    我有一些代码可以检索用户联系人中的所有电话号码 但只想过滤掉手机号码 目前 我只是通过将第一个数字为 或第二个数字为 7 的数字添加到数组中来实现此目的 如下所示 func findContacts gt CNContact let key
  • BigQuery 未显示链接的 Firebase Analytics 事件日志的任何数据集

    我将我的帐户链接到 Big Query 但 Firebase Analytics 事件不会自动加载到 BigQuery 中 显示 未找到数据集 警告 我的工作进度附在下面 请查收 I have getting firebase Analyt
  • jQuery:离线后 POST 出错(iOS 和 Chrome)

    我构建了一个具有离线功能的 HTML5 Web 应用程序 使用 AppCache 程序流程为 Online 在网络上时 应用程序预加载一些基本信息 工作 Offline 用户拿着装有应用程序的平板电脑offline 然后在应用程序上执行他们
  • 使用数组中的字符串淡入/淡出标签

    func setOverlayTitle self overlayLogo text Welcome var hello String Bon Jour GUTEN nMORGEN BONJOUR HOLA BUENOS D AS BUON
  • 将类型传递给通用 Swift 扩展,或者理想情况下推断它

    说你有 class Fancy UIView 你想找到所有兄弟姐妹Fancy意见 没问题 https stackoverflow com q 37232743 294884 for v UIView in superview subview
  • 在 iOS 上使用 RNCryptor 异步解密大文件

    我需要在 iOS 上使用 RNCryptor 异步解密一个大文件 以便显示进度条 我在任何地方都找不到示例 因此尝试了我猜对的方法 但是 我想出的方法不起作用 解密器的处理程序从未被调用 并且线程在发送所有数据后因 EXC BAD ADDR
  • 使用 UITabBarController 时覆盖整个屏幕的视图?

    我想在 UITabBarController 设置中在整个屏幕上覆盖 HUD 样式的透明图形 执行此操作的按钮位于第一个选项卡的屏幕 FirstViewController 中 并且覆盖层也应该覆盖选项卡 这可能吗 您可以将新视图直接附加到
  • UIViewController 内的 UIsearchController 使用自动布局

    有没有人成功实施过UIViewController其中包含两个UISearchController searchBar and a UItableView使用自动布局来布局所有内容 我正在尝试实现类似的目标1密码 https itunes
  • Unwind segue 的用途是什么以及如何使用它们?

    iOS 6 和 Xcode 4 5 有一个称为 Unwind Segue 的新功能 展开转场可以允许过渡到故事板中场景的现有实例 除了 Xcode 4 5 发行说明中的 这个简短条目之外 UIViewController 现在似乎还有几个新
  • 致命错误:在 Swift 中解包可选值时意外发现 nil

    所以我试图获取 Swift 中输入字段的文本 这就是我得到的 class ViewController UIViewController IBOutlet var passwordField UITextField IBOutlet var

随机推荐

  • Flutter VS React Native,应该选哪个?

    移动行业渴望进行一场革命 xff0c 以遏制移动应用程序开发过程中出现的成本高 耗时长等问题 因此 xff0c 该变革以跨平台开发的形式出现 现在 xff0c 维护代码和开发应用程序对于开发人员来说变得简单且省时 那么对于开发者来说 xff
  • VScode如何去打开html页面

    首先在打开VsCode 找到扩展商店 搜索open in browser插件 第二步在html页面中点击右键 找到Open In Default Browser 打开浏览器 Open In Other Browser 选择其他的浏览器打开
  • 2、SPSS的基本知识

    目录 一 SPSS软件的安装和启动 二 SPSS的基本操作环境 xff08 1 xff09 数据编辑窗口 xff08 主程序窗口 xff09 xff08 2 xff09 SPSS结果输出窗口 三 SPSS软件的退出 四 SPSS软件的三种基
  • STM32F429IGT6移植FreeRTOS时遇到的问题汇总

    一 学习环境 编译环境 xff1a keil 开发板 xff1a 野火STM32F429开发板 二 遇到的问题 问题一 xff1a error This port can only be used when the project opti
  • 一篇搞懂关于计算机的减法运算

    一篇搞懂关于计算机的减法运算 减法相减结果为正的减法相减结果为负数的减法 减法 相减结果为正的减法 如下一篇拙言 xff0c 是自己平时的总结 xff0c 如有错误欢迎各位大佬指正 相信你一定听说过 xff0c 补码 xff0c 取反加一等
  • 使用Dev C++建立工程文件调用不同文件下的c文件

    在学校嵌入式软件小组课上直播翻车 xff0c 很尴尬 xff01 xff01 xff01 xff01 然后我结束以后仔细找了一歘啊错误原来是因为没有主一头文件的包含形式导致的 我先介绍一下C语言包含头文件时 lt gt 和 34 34 区别
  • Java Spring Boot 热部署

    一 IDEA配置 当我们修改了Java类后 xff0c IDEA默认是不自动编译的 xff0c 而spring boot devtools又是监测classpath下的文件发生变化才会重启应用 xff0c 所以需要设置IDEA的自动编译 x
  • 新手上路——树莓派3B+系统安装

    首先拿到树莓派后是万分开心 xff0c 索然我不知道他是个什么 xff0c 能用来干什么 xff0c 不过貌似是个电脑主机 xff0c 所以就上手玩儿了 刚拿到手里然后去树莓派基金会网站 xff08 https www raspberryp
  • 新手上路——树莓派3B+安装cmake

    安装smake有两种方法地一种是自动安装 第二种是手动安装 建议使用手动安装 xff0c 自动安装不一定是你想要的版本 xff0c 现介绍第一种 方案一 xff1a sudo apt install cmake 这样就结束了方便 快捷 方案
  • 新手上路——树莓派3B+安装OpenCV(你想要的版本)

    啊啊啊啊啊 xff01 xff01 安装六七次都没成功的我近乎崩溃 xff0c 这次终于成功了 xff0c 失败了无数次的我好像让人来教我一下啊 xff0c 可惜没有 xff0c 还是自己琢磨薄吧 xff0c 为了帮助到更多的人我写了一下我
  • 舵机控制使用

    本文仅介绍固定180度舵机 MG996R舵机 SG90舵机 MG90S舵机等 引脚功能 xff1a 舵机的转动角度跟输入脉冲有关 xff0c 详细看下表 xff1a 可以发现一个规律 xff1a 角度每次增加45度 xff0c 高电平时间相
  • Python之You-Get库学习

    今天学习一下You Get这个第三方库 xff0c 体验一下视频下载的快乐 简介 You Get库是一个基于Python3的视频下载工具 xff0c 支持多数国内外主流视频站点的视频下载 看一下项目主页的README md 真的是十分的ni
  • CMake编译CUDA项目报错

    CMake编译CUDA项目报错 现象解决方法结果 现象 configure后显示如下错误 CMake Error at C Program Files CMake share cmake 3 26 Modules CMakeDetermin
  • 程序设计思维 week11 作业

    A 题意 蒜头君从现在开始工作 xff0c 年薪 NN 万 他希望在蒜厂附近买一套 6060 平米的房子 xff0c 现在价格是 200200 万 假设房子价格以每年百分之 KK 增长 xff0c 并且蒜头君未来年薪不变 xff0c 且不吃
  • windows->wsl&ubuntu config

    ref https blog csdn net weixin 45883933 article details 106085184 安装前配置 ref 适用于 Linux 的 Windows 子系统安装指南 Windows 10 1 启用
  • 线程阻塞与唤醒

    线程阻塞与唤醒的方法如图 xff1a span class hljs keyword package span newThread span class hljs keyword import span java util Scanner
  • Android的死机、重启问题分析方法

    Android的死机 重启问题分析方法 原文链接 xff1a https blog csdn net jinlu7611 article details 50592385 1 死机现象 1 1 死机定义 当手机长时间无法再被用户控制操作时
  • JAVA小白工具人PHPer

    PHP写多了 xff0c 确实很松散 xff0c 什么都是array xff0c 不像java这么资深 xff0c 个个都是大佬 慢慢学习 1 二维数组取某一列的值 php的写法 array column array 39 id 39 ja
  • 当大恒相机采集帧率没有达到理论帧率时

    当大恒相机采集帧率没有达到理论帧率时 xff0c 可设置最大带宽
  • iOS——UITabBarController

    一 UITabBarController简介 1 UITabBarController 对象可以保存多个 视图控制器 并进行切换 xff0c 并且底部还会有一个 工具栏 xff08 UITabBar xff09 xff0c 工具栏里会有多个