iOS开发基础30-UITabBarController
UITabBarController
是 iOS 中常用的用于创建 tabbar 导航界面的控制器,适用于应用的主要功能通过不同的标签页来组织的场景。本文将详细介绍 UITabBarController
的基本用法、属性和方法,并通过示例代码详细展示如何创建和使用 UITabBarController
。
一、UITabBarController概述
UITabBarController
是一个容器视图控制器,用于管理多个子视图控制器,并在底部显示一个标签栏(UITabBar
)。用户可以通过点击标签栏中的标签来切换不同的子视图控制器。
优点
- 简洁:通过标签栏来组织界面,界面简洁明确。
- 用户体验:用户可以方便地在不同功能模块之间进行切换。
- 便捷:系统自带实现,使用便捷,扩展性强。
二、UITabBarController的基本用法
创建UITabBarController
通常,可以通过代码或界面设计器(Storyboard)来创建一个 UITabBarController
。接下来,我们将演示通过代码创建一个带有两个标签页的 UITabBarController
。
1. 初始化UITabBarController
将 UITabBarController
作为应用的主视图控制器:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建 UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
// 创建子视图控制器
UIViewController *firstViewController = [[UIViewController alloc] init];
firstViewController.view.backgroundColor = [UIColor whiteColor];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"First" image:nil tag:0];
UIViewController *secondViewController = [[UIViewController alloc] init];
secondViewController.view.backgroundColor = [UIColor whiteColor];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:1];
// 将子视图控制器添加到 UITabBarController
tabBarController.viewControllers = @[firstViewController, secondViewController];
// 设置主视图控制器为 UITabBarController
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
@end
2. 设置子视图控制器属性
可以为每个子视图控制器设置标题、图标和其他属性:
firstViewController.tabBarItem.title = @"首页";
firstViewController.tabBarItem.image = [UIImage imageNamed:@"home_icon"];
secondViewController.tabBarItem.title = @"设置";
secondViewController.tabBarItem.image = [UIImage imageNamed:@"settings_icon"];
使用 UIStoryboard 创建 UITabBarController
在 Xcode 的 Interface Builder 中,将 UIViewController
替换为 UITabBarController
,然后使用拖动操作将其他视图控制器添加为它的子视图控制器。
三、UITabBarController常见属性和方法
属性
- viewControllers:管理的子视图控制器数组。
@property (nonatomic, copy) NSArray<__kindof UIViewController *> *viewControllers;
- selectedViewController:当前选中的视图控制器。
@property (nonatomic, assign) UIViewController *selectedViewController;
- selectedIndex:当前选中视图控制器的索引。
@property (nonatomic) NSUInteger selectedIndex;
- tabBar:底部的标签栏,类型为
UITabBar
。
@property (nonatomic, readonly) UITabBar *tabBar;
方法
- -setViewControllers:animated::设置标签页的视图控制器。
- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers animated:(BOOL)animated;
- -tabBar:didSelectItem::当标签被选择时的回调方法。
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
- -moreNavigationController:如果
tabBar
中有超过5个标签,系统会自动创建更多标签页。
@property (nonatomic, readonly) UINavigationController *moreNavigationController;
四、示例代码:创建一个四标签 UITabBarController
以下代码展示了如何创建包含四个标签页的 UITabBarController
,每个标签页都有各自的图标和标签名:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
// 第一个标签页
UIViewController *firstViewController = [[UIViewController alloc] init];
firstViewController.view.backgroundColor = [UIColor whiteColor];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@"home_icon"] tag:0];
// 第二个标签页
UIViewController *secondViewController = [[UIViewController alloc] init];
secondViewController.view.backgroundColor = [UIColor whiteColor];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Explore" image:[UIImage imageNamed:@"explore_icon"] tag:1];
// 第三个标签页
UIViewController *thirdViewController = [[UIViewController alloc] init];
thirdViewController.view.backgroundColor = [UIColor whiteColor];
thirdViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Notifications" image:[UIImage imageNamed:@"notifications_icon"] tag:2];
// 第四个标签页
UIViewController *fourthViewController = [[UIViewController alloc] init];
fourthViewController.view.backgroundColor = [UIColor whiteColor];
fourthViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Settings" image:[UIImage imageNamed:@"settings_icon"] tag:3];
// 将视图控制器添加到 tabBarController
tabBarController.viewControllers = @[firstViewController, secondViewController, thirdViewController, fourthViewController];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
五、UITabBarController的原理分析
UITabBarController
通过组合多个视图控制器来实现标签导航,每个子视图控制器都对应一个 UITabBarItem
。底层实现时,UITabBarController
维护一个 viewControllers
数组,存放各个子视图控制器。
切换视图控制器
当用户点击某个标签时,UITabBarController
会通过设置 selectedIndex
属性来切换到对应的视图控制器,并触发相应的视图控制器的生命周期方法,如 viewWillAppear:
和 viewDidAppear:
,确保在视图切换过程中可以进行必要的资源配置和UI更新。
事件响应
UITabBarController
通过实现 UITabBarDelegate
的方法,如 tabBar:didSelectItem:
,来响应用户点击标签的事件。可以在这个回调中执行一些额外的操作,如页面统计、动画效果等。
六、总结
本文详细介绍了 UITabBarController
的基本用法、常见属性和方法,通过掌握 UITabBarController
及其相关控件,可以轻松实现多标签导航界面,为用户提供友好且一致的使用体验。这对于多数应用来说,都是不可或缺的一部分。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!