建立控制器
// 普通控制器
GroupViewController *groupVC = [[GroupViewController alloc] init];
SecondViewController *secondVC = [[SecondViewController alloc] init];
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
FourthViewController *fourthVC = [[FourthViewController alloc] init];
UINavigationController *groupNC = [[UINavigationController alloc] initWithRootViewController:groupVC];
UINavigationController *secondNC = [[UINavigationController alloc] initWithRootViewController:secondVC];
UINavigationController *thirdNC = [[UINavigationController alloc] initWithRootViewController:thirdVC];
UINavigationController *fourthNC = [[UINavigationController alloc] initWithRootViewController:fourthVC];```
@interface AppDelegate () <UITabBarControllerDelegate>
UITabBarController *tabBarVC = [[UITabBarController alloc] init];
tabBarVC.delegate = self;
tabBarVC.selectedIndex = 1;
tabBarVC.viewControllers = @[groupNC, secondNC, thirdNC, fourthNC, uiVC1, uiVC2, uiVC3];
// 自己定义样式 tabBarItem(选中颜色)注意是那种控制器
groupNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"活动" image:[UIImage imageNamed:@"activity"] selectedImage:[UIImage imageNamed:@"微信"]];
// 显示右上角 小圈圈
groupNC.tabBarItem.badgeValue = @"10";
secondNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"影院" image:[UIImage imageNamed:@"cinema"] selectedImage:[UIImage imageNamed:@"通讯录"]];
thirdNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"电影" image:[UIImage imageNamed:@"movie"] selectedImage:[UIImage imageNamed:@"发现"]];
fourthNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"user"] selectedImage:[UIImage imageNamed:@"我"]];
tabBarVC.tabBar.barTintColor = [UIColor yellowColor];
[tabBarVC.tabBar setTintColor:[UIColor greenColor]];
#pragma mark - 选择 tabBar 所控制的控制器,会运行的方法(每次都会运行)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
if (index == 3) {
NSLog(@"four");
}
if (tabBarController.selectedIndex == 2) {
NSLog(@"three");
}
}
#pragma mark - 控制 tabBar 能否够点击
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
return YES;
}
// 获取到全部的 UINavigationBar(project里面全部)
[[UINavigationBar appearance] setBarTintColor:[UIColor purpleColor]];