iOS學習之UITabBarController

來源:互聯網
上載者:User

標籤:

 

一、標籤視圖控制器——UITabBarController

  1、UITabBarController的繼承關係:

@interface UITabBarController : UIViewController<UITabBarDelegate, NSCoding>

  UITabBarController和UINavigationController一樣都是繼承於UIViewController。

  2、建立UITabBarController

  • 程式的添加過程:UIWindow->UITabBarController->UINavigationController->UIViewController
    // 1.建立UITabBarController對象    UITabBarController *mainTabBar = [[UITabBarController alloc] init];    // 2.將TabBarController管理的視圖控制器放到一個數組中    NSArray *viewControllers = [NSArray arrayWithObjects:firestNav, secondNav, thirdNav, fourthNav, nil];    // 3.設定TabBarController的子視圖控制器數組    mainTabBar.viewControllers = viewControllers;    // 4.將根視圖控制器設定成TabBarController    [self.window setRootViewController:mainTabBar];
  • UITabBarController的重要屬性:
  • viewControllers:管理的視圖控制器(NSArray)
  • tabBar:標籤欄
  • selectedIndex:選中的某個tabBarItem
  • delegate:代理(通過協議監聽當前點擊了哪個標籤,程式碼範例)
// 點擊某個標籤時(tabBarItem)時觸發該方法- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{    viewController.tabBarItem.badgeValue = nil;}

二、UITabBar

  1、UITabBar概述

  • UITabBar包含多個UITabBarItem,每一個UITabBarItem對應一個UIViewController。UITabBar的高度是49。
  • 系統最多隻顯示5個UITabBarItem,當UITabBarItem超過5個時系統會自動增加一個更多按鈕,點擊更多按鈕沒有在底部出現的按鈕會以列表的形式顯示出來。(如所示)

     


  •          

  2、UITabBar的屬性:tintColor、barTintColor、映像設定等。

    // TabBar的屬性        // 設定選中的顏色    mainTabBar.tabBar.tintColor = [UIColor greenColor];        // 半透明效果,預設開啟    mainTabBar.tabBar.translucent = NO;        // 設定tabBar的顏色    mainTabBar.tabBar.barTintColor = [UIColor whiteColor];       // 設定提示    thirdVC.tabBarItem.badgeValue = @"99+";    // 改變tabBar的位置(如果Item重疊可能會導致不能點擊,縱向移動的是文字)    [secondVC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(50, 0)];
   // 設定tabBarItem    // 第一種方式:系統樣式    firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];    // 第二種方式:自訂樣式    UIViewController *secondVC = [[UIViewController alloc] init];    secondVC.view.backgroundColor = [UIColor yellowColor];        // 建立圖片對象    // 未選中的圖片            UIImage *secondImage = [UIImage imageNamed:@"carGary"];    // 選中時的圖片      UIImage *secondSelectImage = [UIImage imageNamed:@"carRed"];        // 設定圖片保留原有樣式,不被渲染    secondImage = [secondImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    secondSelectImage = [secondSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];        secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"第二頁" image:secondImage selectedImage:secondSelectImage];

三、自訂tabBar外觀

  UIAppearance協議可以進行一鍵設定所有導航視圖控制器的顏色的操作。

#pragma mark - UIAppearance    // 設定全域外觀    // 通過[UITabBar appearance]得到當前應用的UITabBar對象來設定tabBar的外觀    // 注意:設定全域外觀最好在AppDelegate裡,否則會無效    [[UITabBar appearance] setBarTintColor:[UIColor cyanColor]];    [[UITabBar appearance] setTintColor:[UIColor brownColor]];    // 改變導覽列外觀顏色    [UINavigationBar appearance].barTintColor = [UIColor blackColor];    // 改變導覽列字型顏色    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:16]}];

 

iOS學習之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.