appDelegate.m
#import "AppDelegate.h"
#import "ViewController.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];
[self.window makeKeyAndVisible];
/*——————————————————————————————————————————————————————————————————————————————-*/
ViewController *vc1 = [[ViewController alloc]init];
vc1.view.backgroundColor = [UIColor whiteColor];
vc1.title = @"第一页";
ViewController *vc2 = [[ViewController alloc]init];
vc2.view.backgroundColor = [UIColor whiteColor];
vc2.title = @"第二页";
ViewController *vc3 = [[ViewController alloc]init];
vc3.view.backgroundColor = [UIColor whiteColor];
vc3.title = @"第三页";
UIViewController *vc4 = [[UIViewController alloc]init];
UIViewController *vc5 = [[UIViewController alloc]init];
UIViewController *vc6 = [[UIViewController alloc]init];
UIViewController *vc7 = [[UIViewController alloc]init];
vc4.title = @"4";
vc5.title = @"5";
vc6.title = @"6";
vc7.title = @"7";
//创建标签控制器
UITabBarController *tbc = [[UITabBarController alloc]init];
self.window.rootViewController = tbc;
/*———————————————属性———————————————————————————————————————————————————————————————-*/
//1.设置子控制器数组
tbc.viewControllers = @[vc1,vc2,vc3,vc4,vc5,vc6,vc7];
//标签栏属性
tbc.tabBar.barTintColor = [UIColor grayColor];
//背景颜色 半透明
tbc.tabBar.backgroundColor = [UIColor blueColor];
//背景图片 可以设置拉伸
tbc.tabBar.backgroundImage = [UIImage imageNamed:@"navbar_bg_normal"]; //上面两项看不见
//选中项的颜色
tbc.tabBar.tintColor = [UIColor redColor];
//选中项的背景图片
tbc.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"选中"];
/*——————————————————————————————————————————————————————————————————————————————-*/
vc1.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"d" image:[UIImage imageNamed:@"tab_buddy_nor"]tag:101];
//item的提示信息
vc1.tabBarItem.badgeValue = @"1";
//设置选中的控制器
tbc.selectedIndex = 1;
return YES;
}
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UITabBarControllerDelegate>
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSLog(@"%@",self.tabBarController);
self.tabBarController.delegate =self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",self.tabBarController);
}
#pragma mark -- UITabBarDelegate
//即将选中标签栏上的某个item时调用
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
return YES;
}
//✅选中标签栏上的某个item时调用
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
// NSLog(@"%@",viewController);
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
NSLog(@"%ld",index);
}
//自定义更改子视图数组时
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED{
NSLog(@"will begin custom");
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED{
NSLog(@"will end custom");
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED{
NSLog(@"did end custom");
}
@end