iOSDay28之UITabBarController

來源:互聯網
上載者:User

標籤:

1. 標籤視圖控制器 -- UITabBarController 
  • 視圖(UIView) ---> 圖層 ---> 子視圖
  • 視圖控制器(UIViewController) ---> 管理檢視
  • 導航控制器(UINavigationController) ---> 管理有層次關係的視圖控制器
  • 標籤視圖控制器(UITabBarController) ---> 管理沒有層次關係的視圖控制器
 1> UITabBarController的繼承關係

  @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>

 2> UITabBarController的三層結構

 3> 代碼建立UITabBarController

  在application: idFinishLaunchingWithOptions:方法中建立

  ① 建立Window(需要將工程的主故事版刪除)
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];        self.window.backgroundColor = [UIColor whiteColor];        [self.window makeKeyAndVisible];
  ② 建立UITabBarController對象
 1   UITabBarController  *mainTabBar = [[UITabBarController alloc] init]; 2      3     // 建立控制器對象 4     UIViewController *firstVC = [[UIViewController alloc] init]; 5      6     firstVC.view.backgroundColor = [UIColor cyanColor]; 7      8     // 設定tabBarItem 9     // 第一種方式:系統樣式10     firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];11     12     // 第二種方式:自訂樣式13     UIViewController *secondVC = [[UIViewController alloc] init];14     15     secondVC.view.backgroundColor = [UIColor redColor];16     17     // 建立圖片18     UIImage *secondImage = [UIImage imageNamed:@"carGary"];19     20     UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"];21     22 #pragma mark - 設定圖片保留原有樣式23     secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];24     secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];25     26 #pragma mark -27     secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二頁" image:secondImage selectedImage:secondSelectImage];28     29     // thirdVC30     UIViewController *thirdVC = [[UIViewController alloc] init];31     32     thirdVC.view.backgroundColor = [UIColor purpleColor];33     34     thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"發現" image:[UIImage imageNamed:@"findGray"] tag:101];35     36     // fourthVC37     UIViewController *fourthVC = [[UIViewController alloc] init];38     39     fourthVC.view.backgroundColor = [UIColor greenColor];40     41     fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"userGray"] tag:102];42     43     // fifthVC44     UIViewController *fifthVC = [[UIViewController alloc] init];45     46     fifthVC.view.backgroundColor = [UIColor orangeColor];47     48     fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:103];49     50     // sixthVC51     UIViewController *sixthVC = [[UIViewController alloc] init];52     53     sixthVC.view.backgroundColor = [UIColor magentaColor];54     55     sixthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:104];56     57     // 設定控制器數組58     mainTabBar.viewControllers = @[firstVC, secondVC, thirdVC];
  ③ 將UITabBarController對象設定為Window的根視圖控制器

   1 self.window.rootViewController = mainTabBar;                                                 

 4> UITabBarController的重要屬性

  viewControllers屬性的應用件 3> ② 的代碼

  // 設定進入應用時選中第幾個    mainTabBar.selectedIndex = 1;
2. UITabBar  1> 概述

  UITabBar 包含多個 UITabBarItem , 每個 UITabBarItem 對應一個 UIViewController

  UITabBar 的高度是 49

  系統最多隻顯示 5 個 UITabBarItem , 當 UITabBarItem 超過 5 個時系統會自動增加一個更多按鈕, 點擊更多按鈕, 沒有在底部出現的按鈕會以 列表 的形式顯示出來

  UITabBar的屬性: tintColor , barTintColor , 映像設定等

 2> UItabBar常用的屬性
 1     // tabBar的屬性 2      3     // 設定選中的顏色 4     mainTabBar.tabBar.tintColor = [UIColor greenColor]; 5      6     // 是否開啟半透明效果 7     mainTabBar.tabBar.translucent = NO; 8      9     // 設定tabBar的顏色10 //    mainTabBar.tabBar.barTintColor = [UIColor grayColor];
 3> UITabBarItem
  •  UITabBarItem 可以通過屬性 title , badgeValue 設定標題及提示  

  // 設定提示    thirdVC.tabBarItem.badgeValue = @"有訊息";
  • UITabBarItem 的建立

  ① 系統樣式

    // 第一種方式:系統樣式    firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];

  ② 自訂樣式

// 第二種方式:自訂樣式secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二頁" image:secondImage selectedImage:secondSelectImage];

  secondImage 和 secondSelectImage 是兩個 UIImage 類型的變數

  • UITabBarItem 的圖片處理
1     // 建立圖片2     UIImage *secondImage = [UIImage imageNamed:@"carGary"];3     4     UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"];5     6 #pragma mark - 設定圖片保留原有樣式7     secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];8     secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
3. 自訂tabBar外觀(UIAppearance)

 1> 概述

  如果想通過一鍵設定所有導航試圖控制器的顏色, 類似於QQ的一鍵換膚操作,可以通過UIAppearance 協議 來進行操作, 通過它可以對一些控制項進行定義顏色等。

 2> 使用代碼

1     // 設定全域外觀2     // 通過[UITabBar appearance]得到當前應用的UITabBar對象來設定tabBar的外觀3     // 注意:設定全域外觀最好在appDelegate ,否則會無效4     [[UITabBar appearance] setBarTintColor:[UIColor cyanColor]]; [[UITabBar appearance] setTintColor:[UIColor brownColor]];5     // 改變導覽列外觀顏6     [[UINavigationBar appearance] setBarTintColor:[UIColor lightGrayColor]];7     // 改變導覽列字型顏8     [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:17], NSFontAttributeName, nil]];

 

iOSDay28之UITabBarController

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.