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";
//創(chuàng)建標(biāo)簽控制器
UITabBarController *tbc = [[UITabBarController alloc]init];
self.window.rootViewController = tbc;
/*———————————————屬性———————————————————————————————————————————————————————————————-*/
//1.設(shè)置子控制器數(shù)組
tbc.viewControllers = @[vc1,vc2,vc3,vc4,vc5,vc6,vc7];
//標(biāo)簽欄屬性
tbc.tabBar.barTintColor = [UIColor grayColor];
//背景顏色 半透明
tbc.tabBar.backgroundColor = [UIColor blueColor];
//背景圖片 可以設(shè)置拉伸
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";
//設(shè)置選中的控制器
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
//即將選中標(biāo)簽欄上的某個item時調(diào)用
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
return YES;
}
//?選中標(biāo)簽欄上的某個item時調(diào)用
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
// NSLog(@"%@",viewController);
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
NSLog(@"%ld",index);
}
//自定義更改子視圖數(shù)組時
- (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
屏幕快照 2016-03-08 上午11.47.22.png
屏幕快照 2016-03-08 上午11.47.34.png
屏幕快照 2016-03-08 上午11.48.16.png