UITabBarController实现Tab切换
时间:2022-04-27
本文章向大家介绍UITabBarController实现Tab切换,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
在很多的应用中,基本都是使用Tab方式进行模块间的切换。在Android中很多控件可以实现这类效果,TabLayout、TabHost… 而在iOS中系统为我们提供了UITabBarController类来实现Tab页面之间的切换。有几个页面我们就创建几个UINavigationController,然后通过tabbar.viewControllers = @[navi1, navi2, navi3…]; 设置给UITabBarController。在进行Tab切换的设计思想上Android和iOS是一致的,就是通过控件去管理页面,所以有几个Tab,我们就要创建几个View实例。
项目实例
首先创建3个页面:RedViewController.m、GreenViewController.m、 BlueViewController.m,三个页面的代码一样,只列举其一。 RedViewController.m
#import "RedViewController.h"
@interface RedViewController ()
@end
@implementation RedViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"红色";
self.tabBarItem.title = @"首页";
self.tabBarItem.image = [UIImage imageNamed:@"menu_a"]; //默认图片
self.tabBarItem.selectedImage = [UIImage imageNamed:@"menu_a1"]; //选中后图片
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
@end
创建UITabBarController对象,设置tabbar的子控制器,然后通过self.window.rootViewController = tabbar; 将tabbar赋值给根视图控制器。 代码如下: AppDelegate.m
#import "AppDelegate.h"
#import "RedViewController.h"
#import "GreenViewController.h"
#import "BlueViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//创建tabbar所管理的子控制器,每个子控制器都带有一个导航
RedViewController *redVC = [[RedViewController alloc]initWithNibName:@"RedViewController" bundle:nil];
UINavigationController *navi1 = [[UINavigationController alloc]initWithRootViewController:redVC];
GreenViewController *greenVC = [[GreenViewController alloc]initWithNibName:@"GreenViewController" bundle:nil];
UINavigationController *navi2 = [[UINavigationController alloc]initWithRootViewController:greenVC];
BlueViewController *blueVC = [[BlueViewController alloc]initWithNibName:@"BlueViewController" bundle:nil];
UINavigationController *navi3 = [[UINavigationController alloc]initWithRootViewController:blueVC];
//创建UITabBarController对象
UITabBarController *tabbar = [[UITabBarController alloc]init];
//设置tabbar的子控制器
tabbar.viewControllers = @[navi1, navi2, navi3];
//赋值根控制器
self.window.rootViewController = tabbar;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
- 【译】Spring 官方教程:使用 Restdocs 创建 API 文档
- c#中设置快捷键
- 程序员你为什么这么累【续】:编码习惯之工具类规范
- IntelliJ IDEA 复杂的重构技巧
- 打造属于自己的支持版本迭代的Asp.Net Web Api Route
- 分布式消息队列 RocketMQ 源码分析 —— Message 拉取与消费(上)
- 分布式消息队列 RocketMQ 源码分析 —— Message 拉取与消费(下)
- Spring Boot中使用RabbitMQ
- Spring Cloud构建微服务架构:消息驱动的微服务(入门)【Dalston版】
- 哪类人适合当产品经理?
- 产品经理·杂谈
- Python机器学习中的特征选择
- Android学习第八弹之改变状态栏的颜色使其与APP风格一体化
- 手把手教你 MongoDB 的安装与详细使用(二)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- kubernetes-存储卷(十二)
- kubernetes-配置管理(十一)
- ceph-mimic版
- kubernetes-ingress(十)
- kubernetes-控制器Deployment和DaemonSet(八)
- kubernetes集群网络
- kubernetes-深入理解pod对象(七)
- kubernetes-核心概念及创建应用(六)
- k8s1.13.0二进制部署-Dashboard和coredns(五)
- kubernetes监控-prometheus(十六)
- kubernetes监控-Heapster+InfluxDB+Grafana(十五)
- centos7 安装mysql5.7
- ceph-简介及安装(luminous)版
- Activiti7 查询用户任务列表
- ansible自动化部署工具