UITabBarController詳解(介紹和設定),uitabbarcontroller

來源:互聯網
上載者:User

UITabBarController詳解(介紹和設定),uitabbarcontroller




[objc] view plaincopy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     // Override point for customization after application launch.  
  5.     self.window.backgroundColor = [UIColor whiteColor];  
  6.    
  7. #pragma mark - 設定tabBarItem  
  8. #pragma mark  第一個視圖ViewController  
  9.       
  10.     HMT_AViewController * tabBarViewA = [[HMT_AViewController alloc] init];  
  11.     // 設定A視圖下----標籤欄標題文字(可參照或者QQ體會)  
  12.     tabBarViewA.tabBarItem.title = @"";  
  13.     // 設定A視圖下----標籤欄圖片(因為自己沒有圖片,在這裡隨便設定了個名字)  
  14.     //tabBarViewA.tabBarItem.image = [UIImage imageNamed:@"1.png"];  
  15.     // 設定A視圖下----標籤欄資訊提示(住:badgeValue是NSString類型 如下設定了3,就像QQ訊息有3條未接受一樣,給人一種提醒)  
  16.     tabBarViewA.tabBarItem.badgeValue = @"3";  
  17.     // ios7棄用了----標籤欄選中的時候顯示一張圖片,沒選中的時候顯示另一張圖片  
  18.     //[tabBarViewA.tabBarItem setFinishedSelectedImage:actionMenu.selectedIcon withFinishedUnselectedImage:actionMenu.icon];  
  19.     // ios7的方法(自己沒有圖片,所以代碼裡面的圖片都是一個隨便取的名字,沒有實用意義)  
  20.     //tabBarViewA.tabBarItem.selectedImage = actionMenu.selectedIcon;  
  21.       
  22. #pragma mark  第二個視圖ViewController  
  23.     // 第二個視圖ViewController  
  24.     HMT_BViewController * tabBarViewB = [[HMT_BViewController alloc] init];  
  25.     // 設定B視圖下----標籤欄  
  26.     // 用系統提供的標識(可以算等價於表徵圖和文字)進行設定(參數:UITabBarSystemItem是個枚舉值,想要什麼形式,就去系統提供的API中找)  
  27.     tabBarViewB.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];  
  28.     // 設定B視圖下----標籤欄資訊提示  
  29.     tabBarViewB.tabBarItem.badgeValue = @"GO";  
  30.       
  31. #pragma mark  第三個視圖ViewController  
  32.     HMT_CViewController * tabBarViewC = [[HMT_CViewController alloc] init];  
  33.     tabBarViewC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2];  
  34.     // 設定B視圖下----標籤欄資訊提示  
  35.     tabBarViewC.tabBarItem.badgeValue = @"new";  
  36.       
  37. #pragma mark  第四個視圖ViewController  
  38.     HMT_DViewController * tabBarViewD = [[HMT_DViewController alloc] init];  
  39.     tabBarViewD.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];  
  40.     // 設定B視圖下----標籤欄資訊提示  
  41.     tabBarViewD.tabBarItem.badgeValue = @"99";  
  42.       
  43. #pragma mark  第五個視圖ViewController  
  44.     HMT_EViewController * tabBarViewE = [[HMT_EViewController alloc] init];  
  45.     tabBarViewE.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4];  
  46.     // 設定B視圖下----標籤欄資訊提示  
  47.     tabBarViewE.tabBarItem.badgeValue = @"sky";  
  48.       
  49. #pragma mark  第六個視圖ViewController(系統預設能顯示的最大視圖個數是5個)  
  50.     /* 如果你的viewControllers屬性添加了多於五個的items,那tab bar controller將會自動插入一個特殊的view controller, 
  51.     稱為 More view controller,該 controller 將會負責管理多於的items,這個More view controller提供一個自訂的介面, 
  52.     用table的方式呈現多餘的view controller,並且view controller的數量是不限制的*/  
  53.     HMT_FViewController * tabBarViewF = [[HMT_FViewController alloc] init];  
  54.     tabBarViewF.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:5];  
  55.     // 設定F視圖下----標籤欄資訊提示  
  56.     tabBarViewF.tabBarItem.badgeValue = @"AG";  
  57.       
  58.       
  59.       
  60. #pragma mark - 設定TabBarController  
  61.       
  62.     // 建立TabBarController  
  63.     UITabBarController * tabBarController = [[UITabBarController alloc]init];  
  64.     // TabBarController預設是放在最底部的,如果你想調整位置,可以進行下面2部操作(44是iPhone中TabBarController和UINavigationController標準高度)  
  65.     //CGRect frame = CGRectMake(0, 20, 320, 44);  
  66.     //tabBarController.tabBar.frame = frame;  
  67.     // 每一個tab都必須有一個content view controller------->viewControllers屬性,用來存入一個應用的TabBarController有多少個介面切換  
  68.     tabBarController.viewControllers = [NSArray arrayWithObjects:tabBarViewA,tabBarViewB,tabBarViewC,tabBarViewD,tabBarViewE,tabBarViewF, nil nil];  
  69.     // 設定著色  
  70.     tabBarController.tabBar.tintColor = [UIColor greenColor];  
  71.     // 設定選中圖片時候  
  72.     tabBarController.tabBar.selectedImageTintColor = [UIColor brownColor];  
  73.     // 設定背景圖片(自己沒有圖片,不進行設定)  
  74.     //tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"@@@@@"];  
  75.     // 設定程式啟動時預設的ViewController視圖(設定為3,一共5個ViewController,進來時候顯示的視圖就是第4個-tabBarViewD,下標從0開始)  
  76.     tabBarController.selectedIndex = 3;  
  77.       
  78.       
  79.     self.window.rootViewController = tabBarController;  
  80.       
  81.     [self.window makeKeyAndVisible];  
  82.     return YES;  
  83. }  

@最後效果如:





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];
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.