UITabBarController分欄控制器基本使用
![Uploading 屏幕快照 2017-10-17 16.08.33_728174.png . . .]
屏幕快照 2017-10-17 16.08.33.png
- UITabBarController
- alloc init 創建分欄控制器
- .viewControllers = array 分欄控制器管理數組
- .selectedIndex 設置默認選中的視圖控制器的索引
- .selectedViewController 當前選中的視圖控制器,可根據該屬性進行個性化操作
- .tabBar.translucent 設置分欄控制器的工具欄的透明度
- UITabBarItem
- alloc
- initWithTitle
- initWithTabBarSystemItem 有系統自帶圖標
具體使用:
//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];
//創建一個控制器數組對象
//將所有要被分欄控制器管理的對象添加到數組中
NSArray *arrayVC = [NSArray arrayWithObjects:vcFirst,vcSecond,vcThird, nil];
//將分欄視圖控制器管理數組賦值
tbController.viewControllers = arrayVC;
//將分欄控制器作為根視圖
self.window.rootViewController = tbController;
//設置選中的視圖控制器的索引
//通過索引來確定默認選中的視圖
tbController.selectedIndex = 2;
if(tbController.selectedViewController == vcThird){
NSLog(@"當前選中的是視圖三");
}
//設置分欄控制器的工具欄的透明度
tbController.tabBar.translucent = NO;
return YES;
}
//VCFirst.m
#import "VCFirst.h"
@interface VCFirst ()
@end
@implementation VCFirst
- (void)viewDidLoad {
[super viewDidLoad];
//創建一個分欄按鈕對象 方法一
//P1:顯示的文字
//P2:顯示圖標
//P3:設置按鈕的標記值
// 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協議方法
在分欄控制器欄目超過5個時,就會以“更多”的方式顯示出來,點擊更多,支持控制器位置交換,協議提供了相應事件的響應方法:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 選中控制器對象方法
- tabBarController.selectedIndex 獲得當前選中項的索引
- (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; 即將結束編輯方法
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED; 已經結束編輯方法
- changed==YES 表示順序發生改變
- NSLog(@"%@", viewControllers ); 打印數組查看位置順序