UITabBarController詳解(介紹和設定),uitabbarcontroller
[objc] view plaincopy
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
-
- #pragma mark - 設定tabBarItem
- #pragma mark 第一個視圖ViewController
-
- HMT_AViewController * tabBarViewA = [[HMT_AViewController alloc] init];
- // 設定A視圖下----標籤欄標題文字(可參照或者QQ體會)
- tabBarViewA.tabBarItem.title = @"";
- // 設定A視圖下----標籤欄圖片(因為自己沒有圖片,在這裡隨便設定了個名字)
- //tabBarViewA.tabBarItem.image = [UIImage imageNamed:@"1.png"];
- // 設定A視圖下----標籤欄資訊提示(住:badgeValue是NSString類型 如下設定了3,就像QQ訊息有3條未接受一樣,給人一種提醒)
- tabBarViewA.tabBarItem.badgeValue = @"3";
- // ios7棄用了----標籤欄選中的時候顯示一張圖片,沒選中的時候顯示另一張圖片
- //[tabBarViewA.tabBarItem setFinishedSelectedImage:actionMenu.selectedIcon withFinishedUnselectedImage:actionMenu.icon];
- // ios7的方法(自己沒有圖片,所以代碼裡面的圖片都是一個隨便取的名字,沒有實用意義)
- //tabBarViewA.tabBarItem.selectedImage = actionMenu.selectedIcon;
-
- #pragma mark 第二個視圖ViewController
- // 第二個視圖ViewController
- HMT_BViewController * tabBarViewB = [[HMT_BViewController alloc] init];
- // 設定B視圖下----標籤欄
- // 用系統提供的標識(可以算等價於表徵圖和文字)進行設定(參數:UITabBarSystemItem是個枚舉值,想要什麼形式,就去系統提供的API中找)
- tabBarViewB.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
- // 設定B視圖下----標籤欄資訊提示
- tabBarViewB.tabBarItem.badgeValue = @"GO";
-
- #pragma mark 第三個視圖ViewController
- HMT_CViewController * tabBarViewC = [[HMT_CViewController alloc] init];
- tabBarViewC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2];
- // 設定B視圖下----標籤欄資訊提示
- tabBarViewC.tabBarItem.badgeValue = @"new";
-
- #pragma mark 第四個視圖ViewController
- HMT_DViewController * tabBarViewD = [[HMT_DViewController alloc] init];
- tabBarViewD.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
- // 設定B視圖下----標籤欄資訊提示
- tabBarViewD.tabBarItem.badgeValue = @"99";
-
- #pragma mark 第五個視圖ViewController
- HMT_EViewController * tabBarViewE = [[HMT_EViewController alloc] init];
- tabBarViewE.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4];
- // 設定B視圖下----標籤欄資訊提示
- tabBarViewE.tabBarItem.badgeValue = @"sky";
-
- #pragma mark 第六個視圖ViewController(系統預設能顯示的最大視圖個數是5個)
- /* 如果你的viewControllers屬性添加了多於五個的items,那tab bar controller將會自動插入一個特殊的view controller,
- 稱為 More view controller,該 controller 將會負責管理多於的items,這個More view controller提供一個自訂的介面,
- 用table的方式呈現多餘的view controller,並且view controller的數量是不限制的*/
- HMT_FViewController * tabBarViewF = [[HMT_FViewController alloc] init];
- tabBarViewF.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:5];
- // 設定F視圖下----標籤欄資訊提示
- tabBarViewF.tabBarItem.badgeValue = @"AG";
-
-
-
- #pragma mark - 設定TabBarController
-
- // 建立TabBarController
- UITabBarController * tabBarController = [[UITabBarController alloc]init];
- // TabBarController預設是放在最底部的,如果你想調整位置,可以進行下面2部操作(44是iPhone中TabBarController和UINavigationController標準高度)
- //CGRect frame = CGRectMake(0, 20, 320, 44);
- //tabBarController.tabBar.frame = frame;
- // 每一個tab都必須有一個content view controller------->viewControllers屬性,用來存入一個應用的TabBarController有多少個介面切換
- tabBarController.viewControllers = [NSArray arrayWithObjects:tabBarViewA,tabBarViewB,tabBarViewC,tabBarViewD,tabBarViewE,tabBarViewF, nil nil];
- // 設定著色
- tabBarController.tabBar.tintColor = [UIColor greenColor];
- // 設定選中圖片時候
- tabBarController.tabBar.selectedImageTintColor = [UIColor brownColor];
- // 設定背景圖片(自己沒有圖片,不進行設定)
- //tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"@@@@@"];
- // 設定程式啟動時預設的ViewController視圖(設定為3,一共5個ViewController,進來時候顯示的視圖就是第4個-tabBarViewD,下標從0開始)
- tabBarController.selectedIndex = 3;
-
-
- self.window.rootViewController = tabBarController;
-
- [self.window makeKeyAndVisible];
- return YES;
- }
@最後效果如:
UITabBar與UITabBarController有什不同?
一個是控制項,一個是視圖控制器,視圖控制器裡麵包含了一個tabbar的控制項!當你從一個視圖A 跳轉到B的時候,你如果想讓B具備tabbar的效果,你可以讓B直接繼承自tabbarcontroller 也可以自己用viewcontroller然後在裡面添加tabbar
IOS開發的UITabBarController的一點問題有分
在組裝uitabbarcontroller的代碼中來指定各個uitabbaritem.而不是在各個被包含的viewcontroller中
UITabbarController *tabController=[[UITabbarController alloc] init];
self.vc1=[[UIViewController alloc] init];
slef.vc1.tabbarItem=......;
self.vc2=[[UIViewController alloc] init];
self.vc2.tabbarItem = ....;
......
NSArray *vcs=@[self.vc1,self.vc2,....];
[tabController setViewControllers:vcs];