UITabBarController分欄控制器基本使用
![Uploading 屏幕快照 2017-10-17 16.08.33_728174.png . . .]
屏幕快照 2017-10-17 16.08.33.png
- UITabBarController
- alloc init 創(chuàng)建分欄控制器
- .viewControllers = array 分欄控制器管理數(shù)組
- .selectedIndex 設(shè)置默認(rèn)選中的視圖控制器的索引
- .selectedViewController 當(dāng)前選中的視圖控制器,可根據(jù)該屬性進(jìn)行個(gè)性化操作
- .tabBar.translucent 設(shè)置分欄控制器的工具欄的透明度
- UITabBarItem
- alloc
- initWithTitle
- initWithTabBarSystemItem 有系統(tǒng)自帶圖標(biāo)
具體使用:
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
VCFirst *vcFirst = [[VCFirst alloc]init];
VCSecond *vcSecond = [[VCSecond alloc]init];
vcSecond.view.backgroundColor = [UIColor yellowColor];
VCThird *vcThird = [[VCThird alloc]init];
vcThird.view.backgroundColor = [UIColor orangeColor];
vcFirst.title = @"視圖一";
vcSecond.title = @"視圖二";
vcThird.title = @"視圖三";
vcFirst.view.backgroundColor = [UIColor blueColor];
UITabBarController *tbController = [[UITabBarController alloc]init];
//創(chuàng)建一個(gè)控制器數(shù)組對象
//將所有要被分欄控制器管理的對象添加到數(shù)組中
NSArray *arrayVC = [NSArray arrayWithObjects:vcFirst,vcSecond,vcThird, nil];
//將分欄視圖控制器管理數(shù)組賦值
tbController.viewControllers = arrayVC;
//將分欄控制器作為根視圖
self.window.rootViewController = tbController;
//設(shè)置選中的視圖控制器的索引
//通過索引來確定默認(rèn)選中的視圖
tbController.selectedIndex = 2;
if(tbController.selectedViewController == vcThird){
NSLog(@"當(dāng)前選中的是視圖三");
}
//設(shè)置分欄控制器的工具欄的透明度
tbController.tabBar.translucent = NO;
return YES;
}
//VCFirst.m
#import "VCFirst.h"
@interface VCFirst ()
@end
@implementation VCFirst
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建一個(gè)分欄按鈕對象 方法一
//P1:顯示的文字
//P2:顯示圖標(biāo)
//P3:設(shè)置按鈕的標(biāo)記值
// UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:@"111" image:[UIImage imageNamed:@"icon1"] tag:101];
// self.tabBarItem = tabBarItem;
//方法二
UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:101];
tabBarItem.badgeValue = @"22";
self.tabBarItem = tabBarItem;
}
UITabBarControllerDelegate協(xié)議方法
在分欄控制器欄目超過5個(gè)時(shí)溃列,就會以“更多”的方式顯示出來,點(diǎn)擊更多补鼻,支持控制器位置交換,協(xié)議提供了相應(yīng)事件的響應(yīng)方法:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 選中控制器對象方法
- tabBarController.selectedIndex 獲得當(dāng)前選中項(xiàng)的索引
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; 即將顯示編輯方法
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; 即將結(jié)束編輯方法
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED; 已經(jīng)結(jié)束編輯方法
- changed==YES 表示順序發(fā)生改變
- NSLog(@"%@", viewControllers ); 打印數(shù)組查看位置順序