UITabBarController使用得非常广泛,说说它的核心,UITabBar的使用。
建一个工程,创建4个UIViewController用来作切换的视图控制器,为了方便,就在AppDelegate.m文件里面写代码,如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *fvc = [[FirstViewController alloc]init];
UINavigationController *fNav = [[UINavigationController alloc]initWithRootViewController:fvc];
SecondViewController *svc = [[SecondViewController alloc]init];
UINavigationController *sNav = [[UINavigationController alloc]initWithRootViewController:svc];
ThirdViewController *tvc = [[ThirdViewController alloc]init];
UINavigationController *tNav = [[UINavigationController alloc]initWithRootViewController:tvc];
FourthViewController *fourVc = [[FourthViewController alloc]init];
UINavigationController *fourNav = [[UINavigationController alloc]initWithRootViewController:fourVc];
UITabBarController *tab = [[UITabBarController alloc]init];
tab.viewControllers = @[fNav,sNav,tNav,fourNav];
self.window.rootViewController = tab;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController、SecondViewController、ThirdViewController、FourthViewController是UITabBarController的4个标签控制器,为了区别,设置self.view.backgroundColor为不同颜色,创建UITabBarController对象tab并设置好viewControllers属性,设置tab为window的rootViewController。
UITabBarController *tab = [[UITabBarController alloc]init];
tab.viewControllers = @[fNav,sNav,tNav,fourNav];
self.window.rootViewController = tab;
这时没有设置标签的icon和title,所以tabBar上面什么都没有,但是还是可以点击切换。改变一下TabBar的背景色,设置为黑色:
tab.tabBar.barTintColor = [UIColor blackColor];
效果如下:
下面为添加4个icon和文字,icon和文字是设置在UITabBarItem对象上的,先看看系统自带的图标效果,系统图标和文字是统一的,不用再单独添加文字,修改代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *fvc = [[FirstViewController alloc]init];
UINavigationController *fNav = [[UINavigationController alloc]initWithRootViewController:fvc];
//设置系统tabBarItem1
fvc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
SecondViewController *svc = [[SecondViewController alloc]init];
UINavigationController *sNav = [[UINavigationController alloc]initWithRootViewController:svc];
//设置系统tabBarItem2
svc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];
ThirdViewController *tvc = [[ThirdViewController alloc]init];
UINavigationController *tNav = [[UINavigationController alloc]initWithRootViewController:tvc];
//设置系统tabBarItem3
tvc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:2];
FourthViewController *fourVc = [[FourthViewController alloc]init];
UINavigationController *fourNav = [[UINavigationController alloc]initWithRootViewController:fourVc];
//设置系统tabBarItem4
fourVc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:3];
UITabBarController *tab = [[UITabBarController alloc]init];
tab.viewControllers = @[fNav,sNav,tNav,fourNav];
tab.tabBar.barTintColor = [UIColor blackColor];
self.window.rootViewController = tab;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
效果如下:
系统默认的选中颜色是蓝色,设置tintColor属性,改变选中标签的颜色:
//设置icon的颜色
tab.tabBar.tintColor = [UIColor redColor];
效果如下:
使用自定义图片来设置tabBar,使用initWithTitle:image:tag:方法,苹果官方文档说明图片的大小为30*30,Retina屏为60*60(stackoverflow上有大神指出,UITabBarItem图标的最佳尺寸是48*32):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *fvc = [[FirstViewController alloc]init];
UINavigationController *fNav = [[UINavigationController alloc]initWithRootViewController:fvc];
//设置自定义图片 tabBarItem1
fvc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] tag:0];
SecondViewController *svc = [[SecondViewController alloc]init];
UINavigationController *sNav = [[UINavigationController alloc]initWithRootViewController:svc];
//设置自定义图片 tabBarItem2
svc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"消息" image:[UIImage imageNamed:@"message"] tag:1];
ThirdViewController *tvc = [[ThirdViewController alloc]init];
UINavigationController *tNav = [[UINavigationController alloc]initWithRootViewController:tvc];
//设置自定义图片 tabBarItem3
tvc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"购物车" image:[UIImage imageNamed:@"car"] tag:2];
FourthViewController *fourVc = [[FourthViewController alloc]init];
UINavigationController *fourNav = [[UINavigationController alloc]initWithRootViewController:fourVc];
//设置自定义图片 tabBarItem4
fourVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"个人中心" image:[UIImage imageNamed:@"customer"] tag:3];
UITabBarController *tab = [[UITabBarController alloc]init];
tab.viewControllers = @[fNav,sNav,tNav,fourNav];
tab.tabBar.barTintColor = [UIColor blackColor];
//设置icon的颜色
tab.tabBar.tintColor = [UIColor redColor];
self.window.rootViewController = tab;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}