1.定义:它能选择进入某个页面,通过一个数组里面存储的视图控制器。它也是一个ViewController。
2.初始化菜单栏:
// 菜单栏
UITabBarController *tabbar = [[UITabBarController alloc]init];
tabbar.tabBar.tintColor = [UIColor whiteColor];
tabbar.viewControllers = @[group,news,user];
3.第二点中红字表示 tabbar 中包含的三个 ViewController,他们来自于:
ViewController *group = [[ViewController alloc]init];
NewsViewController *news = [[NewsViewController alloc]init];
UserViewController *user = [[UserViewController alloc]init];
4.找到UITabBarController里面的 菜单栏视图 再找到里面的 tabBarItem 的数组 数组里面存的是 viewControllers 视图控制器的数组 对应的按钮
NSArray *titleName = @[@"圈子",@"新闻",@"个人中心"];
NSArray *photos = @[@"12",@"444",@"333"];
// 设置UITabBarItem的标题
for (int i = 0; i < title.count; i++) {
UITabBarItem *groupItem = tabbar.tabBar.items[i];
groupItem.title = titleName [i];
groupItem.image = [ UIImage imageNamed:photos[i] ];
}
// 让tabbar默认选择在数组下标为1,也就是中间的位置。
tabbar.selectedIndex = 1;
self.window.rootViewController = tabbar; //让tabbar这个实例化出来的对象作为根视图控制器
[self.window makeKeyAndVisible];
带导航控制器的菜单栏
使用方法:
ViewController *group = [[ViewController alloc]init];
UINavigationController *groups = [[UINavigationController alloc]initWithRootViewController:group];
NewsViewController *news = [[NewsViewController alloc]init];
UINavigationController *new = [[UINavigationController alloc]initWithRootViewController:news];
UserViewController *user = [[UserViewController alloc]init];
UINavigationController *use = [[UINavigationController alloc]initWithRootViewController:user];
// 菜单栏
UITabBarController *tabbar = [[UITabBarController alloc]init];
tabbar.tabBar.tintColor = [UIColor whiteColor];
tabbar.viewControllers = @[groups,new,use];
tabbar.viewControllers数组里面的内容变成了导航控制器的对象。