UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换。
#import "AppDelegate.h"
#import "ViewController.h"
#import "SecondViewController.h"
UITabBarController *tabbar = [[UITabBarController alloc]init];//初始化 UITabBarController
ViewController *v= [[ViewController alloc]init];
SecondViewController *sec = [[SecondViewController alloc]init];
sec.tabBarItem.title = @"联系人";
sec.tabBarItem.image =[UIImage imageNamed:@"avatar"];
UIImage *image = [[UIImage imageNamed:@"addPhoto"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//通过该方式避免系统不能识别自定义图片,而显示成系统的默认颜色
ViewController *v= [[ViewController alloc]init];
SecondViewController *sec = [[SecondViewController alloc]init];
sec.tabBarItem.title = @"联系人";
sec.tabBarItem.image =[UIImage imageNamed:@"avatar"];
UIImage *image = [[UIImage imageNamed:@"addPhoto"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//通过该方式避免系统不能识别自定义图片,而显示成系统的默认颜色
UIImage *selecimage = [[UIImage imageNamed:@"addPhoto"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//同上
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"消息" image:image selectedImage:selecimage];
item.badgeValue = @"20";//item右上角数字,相当于QQ消息提示未读消息
item.badgeValue = @"20";//item右上角数字,相当于QQ消息提示未读消息
sec.tabBarItem = item;
tabbar.viewControllers = @[v,sec];//设置UITabBarController的标签个数计对应的视图控制器
tabbar.selectedIndex = 1;//设置选中第几个标签(默认是0)
tabbar.selectedViewController = sec;//设置默认选中的视图控制器(必须存在于viewcontrollers)
tabbar.tabBar.tintColor = [UIColor yellowColor];//设置选中时颜色(包括图片和文字)
tabbar.tabBar.barTintColor = [UIColor redColor];//设置背景色
tabbar.tabBar.backgroundColor = [UIColor greenColor];//设置背景色(颜色淡,在其下)
tabbar.selectedViewController = sec;//设置默认选中的视图控制器(必须存在于viewcontrollers)
tabbar.tabBar.tintColor = [UIColor yellowColor];//设置选中时颜色(包括图片和文字)
tabbar.tabBar.barTintColor = [UIColor redColor];//设置背景色
tabbar.tabBar.backgroundColor = [UIColor greenColor];//设置背景色(颜色淡,在其下)
tabbar.tabBar.backgroundImage = [UIImage imageNamed:@"2.jpg"];//设置背景图片,注意图片尺寸的合理性
self.window.rootViewController = tabbar;
#import "ViewController.h"
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.title = @"联系人";
self.tabBarItem.image = [UIImage imageNamed:@"avatar"];