1. 程式人生 > >iOS開發之UITabBarController的建立和使用

iOS開發之UITabBarController的建立和使用

UITabBarController是一個容器控制器,繼承於UIViewController,一般情況下作為window的根檢視控制器

UITabBarController自帶一個UITabBar

首先建立一個UItabBarController

UITabBarController* barTC =[[UITabBarControlleralloc]init];

作為window的根檢視

self.window.rootViewController= barTC;

設定標題為首頁

 barTC.navigationItem.title= @"首頁";

建立5個檢視控制器

FirstViewController * firstVC =[[FirstViewControlleralloc]init];

SecondViewController * secondVC =[[SecondViewControlleralloc]init];

ThirdViewController * thirdVC =[[ThirdViewControlleralloc]init];

FourViewController * fourVC =[[FourViewControlleralloc]init];

FiveViewController* fiveVC  =[[FiveViewController

alloc]init];

再給5個檢視新增導航控制器

UINavigationController* navigationVC1 =[[UINavigationControlleralloc]initWithRootViewController:firstVC];

UINavigationController * navigationVC2 =[[UINavigationControlleralloc]initWithRootViewController:secondVC];

UINavigationController* navigationVC3 =[[UINavigationController

alloc]initWithRootViewController:thirdVC];

 UINavigationController* navigationVC4 =[[UINavigationControlleralloc]initWithRootViewController:fourVC];

 UINavigationController* navigationVC5 =[[UINavigationControlleralloc]initWithRootViewController:fiveVC];

//UITabBarController的子檢視控制器

    barTC.viewControllers=@[navigationVC1,navigationVC2,navigationVC3,navigationVC4,navigationVC5];

  firstVC.view.backgroundColor= [UIColorredColor];

   firstVC.tabBarItem.title= @"首頁";

 //設定角標

   firstVC.tabBarItem.badgeValue= @"33";

 //UItabBar會對圖片進行渲染,如果不想渲染的話就需要使用(UIImageRenderingModeAlwaysOriginal)。

   firstVC.tabBarItem.image= [[UIImageimageNamed:@"home.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   secondVC.view.backgroundColor= [UIColorblackColor];

   secondVC.tabBarItem.title= @"發現";

   secondVC.tabBarItem.image= [[UIImageimageNamed:@"find.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   thirdVC.view.backgroundColor= [UIColorwhiteColor];

   thirdVC.tabBarItem.title= @"設定";

   thirdVC.tabBarItem.image= [[UIImageimageNamed:@"setting.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   fourVC.view.backgroundColor= [UIColorgrayColor];

   fourVC.tabBarItem.title= @"購物";

   fourVC.tabBarItem.image= [[UIImageimageNamed:@"shopping.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

   fiveVC.view.backgroundColor= [UIColorgreenColor];

   fiveVC.tabBarItem.title= @"聯絡人";

   fiveVC.tabBarItem.image= [UIImageimageNamed:@"contact.png"];

//

//   firstVC.tabBarItem = [[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemBookmarks)tag:1]autorelease];

//   firstVC.tabBarItem.badgeValue [email protected]"23";

//   secondVC.tabBarItem  =[[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemContacts)tag:2]autorelease];

//   thirdVC.tabBarItem = [[[UITabBarItemalloc]initWithTabBarSystemItem:(UITabBarSystemItemDownloads)tag:3]autorelease] ;

//   fourVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"發現" image:[UIImage imageNamed:@"find.png"]selectedImage:[UIImageimageNamed:@"map.png"]]autorelease];

//   fiveVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"首頁" image:[UIImage imageNamed:@"home.png"]selectedImage:[UIImage imageNamed:nil]]autorelease];

//   SixViewController * sixVC = [[SixViewControlleralloc]init];

//   sixVC.tabBarItem = [[[UITabBarItemalloc]initWithTitle:@"猴子" image:[UIImage imageNamed:@"monkey.png"]selectedImage:nil]autorelease];

效果圖如下:



三大檢視控制器組成的框架結構--UItabBarController、UINavigationController、UITableViewController

    第一種--UINavigationController管理UITabBarController,這種結構UINavigationBar上面的內容是不變的。

    第二種結構---UItabBarController管理UINavigationController,這種結構每一個具體的檢視控制器可以設定自己在navigationBar上面的內容

    主要事項:1.如果要給tabBarItem,應該給對應的navigationcontroller設定

    2.如果設定navigationBarItem,應該給具體的檢視控制器設定。一般情況下會在檢視控制器的內部方法裡面設定

UITabBarControllerDelegate協議裡面有如下方法,要定義代理物件為自身   barTC.delegate=self;

- (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController

相關推薦

iOS開發UITabBarController建立使用

UITabBarController是一個容器控制器,繼承於UIViewController,一般情況下作為window的根檢視控制器 UITabBarController自帶一個UITabBar

iOS開發給navigationbarview設定同一張圖片

###### 效果一: 今天因為這個小東西被吐槽了...但還是決定記錄一下... 其實說是給navigationbar和view設定同一張圖片是不對的,應該是隱藏navigationbar,再去掉statusbar的背景色,新增view 最開始

iOS開發32位64位系統

參考網址:http://blog.csdn.net/sakulafly/article/details/41089411 在蘋果推出iPhone5S時,64位的應用就走到了眼前。當時就看見蘋果官方資料宣佈iOS7.x的SDK支援了64位的應用,而且內建的應用都已經是64

IOS開發 ---- 振動強度時間(私有api)

void AudioServicesStopSystemSound(int); void AudioServicesPlaySystemSoundWithVibration(int, id, NSDictionary *); 3.呼叫  NSMutableDictionary* dict = [N

IOS開發----常用函式常數

轉載自:http://blog.sina.com.cn/s/blog_71715bf80101bnvn.html 介紹一下Objective-c常用的函式,常數變數 算術函式 【算術函式】函式名 說明 int rand() 隨機數生成。 (例) srand(ti

(轉)IOS開發----常用函式常數

介紹一下Objective-c常用的函式,常數變數 算術函式 【算術函式】函式名 說明 int rand() 隨機數生成。 (例) srand(time(nil)); //隨機數初期化 int val = rand()P; //0~49之間的隨機數 int abs

(0016)iOS 開發Mac上Navicat Premium 建立遠端連線本地連線

1.下載安裝 (百度雲盤裡面有安裝檔案和註冊機)   連結: https://pan.baidu.com/s/1kVG1k71 密碼: mr5g 2.破解之後還可以漢化: 把解壓的漢化中文包中的兩

ios開發--CGRect/CGSize/CGPoint/CGVector/CGAffineTransform/UIEdgeInsets/UIOffsetNSString之間的轉換

距離 轉換 nsstring transform ans ins -c cto cnblogs 僅做記錄,一個函數和字符串之間的互相轉換 方法如下: UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UI

ios開發--tableview刷新某一個區某一行

anim ios開發 xpath set nsa sat array 代碼 tableview 在開發中,有時候,我們不需要刷新整個表,只需要刷新局部數據即可,具體代碼如下: //section刷新 NSIndexSet *indexSet=[[NSIndexSe

iOS開發根據按鈕確定點選的section row

  分組的tableview cell上面有按鈕,根據按鈕確定點選的是哪一組的哪一行。對於初學者來說不好判斷,那麼下面就來跟大家分享一下如何實現吧。   cell 方法裡面新增   [cell.pingjiaBtn addTarget:self action:@selector(didPingJiaBut

iOS開發根據按鈕確定點擊的section row

get ott 實現 事件 oid self. all 初學者 shopping 分組的tableview cell上面有按鈕,根據按鈕確定點擊的是哪一組的哪一行。對於初學者來說不好判斷,那麽下面就來跟大家分享一下如何實現吧。   cell 方法裏面添加   [cell.p

iOS開發純Weex+Vuejs開發原生App整體結構除錯步驟(二)

前言 上一篇文章是根據Weex官方的介紹,啟動了Weex的Hello World的介紹以及一些外掛的安裝 環境部署和Debug 之前花了2周時間看了下Vue的語法和用法以及一些實戰介紹,畢竟是iOS開發,還是有必要把新學的東西寫下來,而且會寫的非常詳細,能保證從我git倉庫弄下來的基礎結構

iOS開發Weex爬坑路環境部署Devtools Debug(一)

前言 不多說,直接開始Weex,算是記錄這段時間對新技術的學習積累。期間看了很多Vue.js和Node.js的基礎,順便捋了以下CSS的flex-box佈局等等前端的知識,太多了太雜了,還是用筆記記錄下。直接從官方介紹,開始 補充一個傳送門:和文章沒什麼關係 JS中的async和await

iOS開發AutoLayout中的Content Hugging Priority Content Compression Resistance Priority解析

本篇部落格的內容也不算太複雜,算是AutoLayout的一些高階的用法。本篇部落格我們主要通過一些示例來看一下AutoLayout中的Content Hugging Priority以及Content Compression Resistance Priority這兩個優先順序。下方我們先來簡單的看一下這兩個

iOS開發設定導航欄標題的字型大小顏色

方法一:自定義檢視。 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];

iOS開發NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END

    最近使用新更新的Xcode10.0新建類時,發現它自動為我們在.h檔案裡添加了NS_ASSUME_NONNULL_BEGIN和 NS_ASSUME_NONNULL_END。如下示例: #import <UIKit/UIKit.h> NS_ASSUME_

iOS開發獲取iPhone/iPad/Android 介面icon尺寸規範

注意:iOS所有圖示的圓角效果由系統生成,給到的圖示本身不能是圓角的。1. 桌面圖示 (app icon)for iPhone6 plus(@3x) : 180 x 180for iPhone 6/5s

iOS開發建立音訊播放的單例類

因為涉及到專案的開發,所以這裡只把PlayerCenter單例類中的程式碼進行展示,僅做參考,其中包含上一曲,暫停/播放,下一曲等功能,專案中也涉及到了後臺播放和操作的功能,具體的使用大家可以繼續研究,或者給我留言,等有空寫一個Demo給大家分享PlayCenter.h#im

iOS開發opencv學習筆記一:下載安裝

1. opencv是什麼? opencv是一個基於BSD開源協議的影象處理開源庫,截止本人編輯時間:2017年6月1日,最新版本為3.2.0。 2.哪裡可以得到opencv的原始碼以及不同平臺的動態庫? opencv的官方地址為http://opencv.org/,可以在這

iOS開發-收集的不常用卻實用的小方法技巧

1.顏色轉變成圖片 - (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginI