起首我們看一下它的view層級圖:
- (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;
}
最初後果以下圖:
UITabBarController的署理辦法和模態顯示
起首要完成協定<UITabBarControllerDelegate>
// 設置署理
tabBarController.delegate =self;
//UINavigationController *nav = tabBarController.moreNavigationController;
//[nav setNavigationBarHidden:YES animated:YES];
// 掌握哪些ViewController的標簽欄能被點擊
- (BOOL)tabBarController:(UITabBarController *)tabBarControllershouldSelectViewController:(UIViewController *)viewController{
// 代表HMT_CViewController這個View沒法顯示,沒法點擊到它代表的標簽欄
if ([viewControllerisKindOfClass:[HMT_CViewControllerclass]]) {
returnNO;
}
returnYES;
}
// 選中哪一個標簽欄,一個監控感化吧
- (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController{
}
// More view controller將要開端編纂
- (void)tabBarController:(UITabBarController *)tabBarControllerwillBeginCustomizingViewControllers:(NSArray *)viewControllers{
}
// More view controller將要停止編纂
- (void)tabBarController:(UITabBarController *)tabBarControllerwillEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
}
// More view controller編纂
- (void)tabBarController:(UITabBarController *)tabBarControllerdidEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
}
#import "HMT-AViewController.h"
#import "HMTModalShowViewController.h"
@interfaceHMT_AViewController ()
@end
@implementation HMT_AViewController
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor = [UIColorredColor];
// 創立一個按鈕
UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame =CGRectMake(100,100,100, 100);
[button addTarget:self action:@selector(modalShow)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view.
}
- (void)modalShow{
HMTModalShowViewController * modalShowVC = [[HMTModalShowViewController alloc]init];
//模態視圖掌握器出現出來時刻的視覺後果
modalShowVC.modalTransitionStyle =UIModalTransitionStyleCrossDissolve;
/*
UIModalTransitionStyleCoverVertical = 0, //默許,由下往上
UIModalTransitionStyleFlipHorizontal, //程度遷移轉變後果
UIModalTransitionStyleCrossDissolve, //突變後果
UIModalTransitionStylePartialCurl, //冊頁往上翻動後果
*/
//模態視圖掌握器出現方法,默許全屏
modalShowVC.modalPresentationStyle =UIModalPresentationFullScreen;
/*
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
UIModalPresentationCustom,
UIModalPresentationNone = -1,
*/
UINavigationController * modalShowNC = [[UINavigationController alloc] initWithRootViewController:modalShowVC];
//推出模態視圖掌握器
[self presentViewController:modalShowNC animated:YES completion:^{
NSLog(@"hello world");
}];
}
#import "HMTModalShowViewController.h"
@interfaceHMTModalShowViewController ()
@end
@implementation HMTModalShowViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
// 應用UINavigationController來完成加入掌握器
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(modalDismiss)];
self.navigationItem.leftBarButtonItem = barButton;
self.navigationItem.title =@"humingtao";
//創立一個按鈕來完成加入掌握器
/* UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self action:@selector(modalDismiss) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];*/
}
- (void)modalDismiss{
//加入模態視圖掌握器
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"加入GoodBye");
}];
}
@end
【iOS開辟中UITabBarController的應用示例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!