iOS-CYLTabBarController【好用的TabbarController】

来源:http://www.cnblogs.com/wangkejia/archive/2017/12/01/7943415.html
-Advertisement-
Play Games

用TabbarController進行模塊分類和管理,這裡推薦一個CYLTabBarController,只需兩個數組就可以使用和管理; 1.導入CYLTabBarController 使用cocoapods導入即可 pod 'CYLTabBarController', '~> 1.14.1',終端 ...


用TabbarController進行模塊分類和管理,這裡推薦一個CYLTabBarController,只需兩個數組就可以使用和管理;

 

1.導入CYLTabBarController

使用cocoapods導入即可 pod 'CYLTabBarController', '~> 1.14.1',終端 pod install 即可

 

2.配置

新建一個基於 NSObject 類 MainTabBarControllerConfig,用於配置CYLTabBarController及管理;頭文件引入

#import <CYLTabBarController.h>

.h

@interface MainTabBarControllerConfig : NSObject
///CYLTabBarController @property (nonatomic,strong) CYLTabBarController
*mainTabBarController; @end

.m

@implementation MainTabBarControllerConfig

- (CYLTabBarController *)mainTabBarController{
    if (!_mainTabBarController) {
        UIEdgeInsets imageInsets = UIEdgeInsetsZero;
        UIOffset titlePositionAdjustment = UIOffsetZero;
        _mainTabBarController = [CYLTabBarController tabBarControllerWithViewControllers:[self arrayViewControllerItem] tabBarItemsAttributes:[self arrayAttributesItem] imageInsets:imageInsets titlePositionAdjustment:titlePositionAdjustment];
        [self customizeTabBarAppearance:_mainTabBarController];
    }
    return _mainTabBarController;
}

- (NSArray *)arrayViewControllerItem{
    BookcaseNavViewController *bookcaseNavi = [wkj_getSboardBookcase instantiateViewControllerWithIdentifier:@"BookcaseNavViewController"];
    AskKaoLaNavViewController *askKlNavi = [wkj_getSboardAskKaoLa instantiateViewControllerWithIdentifier:@"AskKaoLaNavViewController"];
    DiscoverNavViewController *discoverNavi = [wkj_getSboardDiscover instantiateViewControllerWithIdentifier:@"DiscoverNavViewController"];
    UserNavViewController *userNavi = [wkj_getSboardUser instantiateViewControllerWithIdentifier:@"UserNavViewController"];
    ///返回需要載入的模塊
    return @[bookcaseNavi,discoverNavi,askKlNavi,userNavi];
}

- (NSArray *)arrayAttributesItem{
    NSDictionary *bookcaseItemsAttributes =@{CYLTabBarItemTitle : @"nav1",
                                                CYLTabBarItemImage : @"bar_tk_01",
                                                /* NSString and UIImage are supported*/
                                                CYLTabBarItemSelectedImage : @"bar_tk_02",};
    
    NSDictionary *discoverItemsAttributes = @{CYLTabBarItemTitle : @"nav2",
                                              CYLTabBarItemImage : @"bar_kc_01",
                                              CYLTabBarItemSelectedImage : @"bar_kc_02",};
    
    NSDictionary *askklItemsAttributes = @{CYLTabBarItemTitle : @"nav3",
                                           CYLTabBarItemImage : @"bar_dzs_01",
                                           CYLTabBarItemSelectedImage : @"bar_dzs_02",};
    
    NSDictionary *userItemsAttributes = @{CYLTabBarItemTitle : @"nav4",
                                          CYLTabBarItemImage : @"bar_wd_01",
                                          CYLTabBarItemSelectedImage : @"bar_wd_02"};
    
    NSArray *tabBarItemsAttributes = @[bookcaseItemsAttributes,
                                       discoverItemsAttributes,
                                       askklItemsAttributes,
                                       userItemsAttributes];
    return tabBarItemsAttributes;
}


/**
 *  更多TabBar自定義設置:比如:tabBarItem 的選中和不選中文字和背景圖片屬性、tabbar 背景圖片屬性等等
 */
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
    // Customize UITabBar height
    // 自定義 TabBar 高度
    //     tabBarController.tabBarHeight = CYLTabBarControllerHeight;
    
    // set the text color for unselected state
    // 普通狀態下的文字屬性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    // set the text color for selected state
    // 選中狀態下的文字屬性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = Wonderful_BlueColor6;
    
    // set the text Attributes
    // 設置文字屬性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    // Set the dark color to selected tab (the dimmed background)
    // TabBarItem選中後的背景顏色
    // [self customizeTabBarSelectionIndicatorImage];
    
    // update TabBar when TabBarItem width did update
    // If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight,
    // remove the comment '//'
    // 如果你的App需要支持橫豎屏,請使用該方法移除註釋 '//'
    // [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
    
    // set the bar shadow image
    // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
    
    // set the bar background image
    // 設置背景圖片
    //     UITabBar *tabBarAppearance = [UITabBar appearance];
    //     [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tab_bar"]];
    
    // remove the bar system shadow image
    // 去除 TabBar 自帶的頂部陰影
    // [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
}

 

3.使用

在AppDelegate.m  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    /載入中間自定義按鈕
//    [TabbarPlusButton registerPlusButton];
    MainTabBarControllerConfig *tabbarConfig = [[MainTabBarControllerConfig alloc]init];
    CYLTabBarController *mainTabbarController = tabbarConfig.mainTabBarController;
    [self.window setRootViewController:mainTabbarController];
    return YES;
}

 

 

 

4.自定義按鈕

加入中間按鈕之前,確保上面的功能已經實現;新建一個基於 CYLPlusButton 的類 TabbarPlusButton,實現代理 CYLPlusButtonSubclassing

 .h

@interface TabbarPlusButton : CYLPlusButton<CYLPlusButtonSubclassing>

@end

.m

@implementation TabbarPlusButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.adjustsImageWhenHighlighted = NO;
    }
    return self;
}

//上下結構的 button
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 控制項大小,間距大小
    // 註意:一定要根據項目中的圖片去調整下麵的0.7和0.9,Demo之所以這麼設置,因為demo中的 plusButton 的 icon 不是正方形。
    CGFloat const imageViewEdgeWidth   = self.bounds.size.width * 0.7;
    CGFloat const imageViewEdgeHeight  = imageViewEdgeWidth * 0.9;
    
    CGFloat const centerOfView    = self.bounds.size.width * 0.5;
    CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;
    CGFloat const verticalMargin  = (self.bounds.size.height - labelLineHeight - imageViewEdgeHeight) * 0.5;
    
    // imageView 和 titleLabel 中心的 Y 值
    CGFloat const centerOfImageView  = verticalMargin + imageViewEdgeHeight * 0.5;
    CGFloat const centerOfTitleLabel = imageViewEdgeHeight  + verticalMargin * 2 + labelLineHeight * 0.5 + 5;
    
    //imageView position 位置
    self.imageView.bounds = CGRectMake(0, 0, imageViewEdgeWidth, imageViewEdgeHeight);
    self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
    
    //title position 位置
    self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
    self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
}

#pragma mark -
#pragma mark - CYLPlusButtonSubclassing Methods

/*
 *
 Create a custom UIButton with title and add it to the center of our tab bar
 *
 */
+ (id)plusButton {
    TabbarPlusButton *button = [[TabbarPlusButton alloc] init];
///中間按鈕圖片 UIImage
*buttonImage = [UIImage imageNamed:@"post_normal"]; [button setImage:buttonImage forState:UIControlStateNormal]; [button setTitle:@"發佈" forState:UIControlStateNormal]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [button setTitle:@"發佈" forState:UIControlStateSelected]; [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected]; button.titleLabel.font = [UIFont systemFontOfSize:9.5]; [button sizeToFit]; // or set frame in this way `button.frame = CGRectMake(0.0, 0.0, 250, 100);` // button.frame = CGRectMake(0.0, 0.0, 250, 100); // button.backgroundColor = [UIColor redColor]; // if you use `+plusChildViewController` , do not addTarget to plusButton. [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside]; return button; } /* * Create a custom UIButton without title and add it to the center of our tab bar * */ //+ (id)plusButton //{ // // UIImage *buttonImage = [UIImage imageNamed:@"hood.png"]; // UIImage *highlightImage = [UIImage imageNamed:@"hood-selected.png"]; // // CYLPlusButtonSubclass* button = [CYLPlusButtonSubclass buttonWithType:UIButtonTypeCustom]; // // button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin; // button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); // [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; // [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted]; // [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside]; // // return button; //} #pragma mark - #pragma mark - Event Response - (void)clickPublish { CYLTabBarController *tabBarController = [self cyl_tabBarController]; UIViewController *viewController = tabBarController.selectedViewController; UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"從相冊選取", @"淘寶一鍵轉賣", nil]; [actionSheet showInView:viewController.view]; } #pragma mark - UIActionSheetDelegate - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex = %@", @(buttonIndex)); } #pragma mark - CYLPlusButtonSubclassing //+ (UIViewController *)plusChildViewController { // UIViewController *plusChildViewController = [[UIViewController alloc] init]; // plusChildViewController.view.backgroundColor = [UIColor redColor]; // plusChildViewController.navigationItem.title = @"PlusChildViewController"; // UIViewController *plusChildNavigationController = [[UINavigationController alloc] // initWithRootViewController:plusChildViewController]; // return plusChildNavigationController; //} // //+ (NSUInteger)indexOfPlusButtonInTabBar { // return 4; //} // //+ (BOOL)shouldSelectPlusChildViewController { // BOOL isSelected = CYLExternPlusButton.selected; // if (isSelected) { // NSLog(@"
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文來自於騰訊Bugly公眾號(weixinBugly),作者:sparrowchen,未經作者同意,請勿轉載,原文地址: http://mp.weixin.qq.com/s/hBgvPBP12IQ1s65ru paWw 1.組件介紹 Page是企鵝FM研發的分頁組件,包括支持分頁非交互切換(通過方 ...
  • UINavigationBar的一些顏色設置,以前老是忘,這次記住了 ...
  • 一,效果圖。 二,工程圖。 三,代碼。 ...
  • 這篇教程的主要目的是解釋怎麼樣在你的iOS工程中創建並使用一個SDK,俗稱.a文件。 環境:xcode 9.0 創建一個靜態庫工程 打開Xcode,點擊File\New\Project, 選擇iOS\Framework and Library\Cocoa Touch Static Library新建 ...
  • 最近在搗鼓小程式,期間遇到的問題,踩過的坑,也是在網上各種搜。這裡也說下我解決的問題,方便大家。 在小程式首頁顯示文本列表的時候,為了美觀,不希望把所有的文本都顯示出來,希望是顯示前幾行(比如前3行,然後加上省略號),網上的大部分做法是從客戶端(client)來考慮的,一開始我也試圖從客戶端來解決問 ...
  • 在做項目的過程中,遇到了一個需求,在Android代碼里可以通過圖片的ID動態載入圖片。要載入Android文件夾裡面的圖片,我們就要知道它的ID,要動態獲取,那麼就得動態獲取圖片的ID。 那麼應該怎麼做呢? 我們可以用 接下來我們講講動態獲取其他資源ID的方法 1、 獲取佈局資源ID int la ...
  • Base64是網路上最常見的用於傳輸8Bit位元組碼的編碼方式之一,Base64就是一種基於64個可列印字元來表示二進位數據的方法。 Base64編碼是從二進位到字元的過程,可用於在HTTP環境下傳遞較長的標識信息。例如,在Java Persistence系統Hibernate中,就採用了Base64 ...
  • 眾所周知,日誌的管理是軟體系統很重要的一部分,千萬不可忽略其重要性。完整的日誌將會在系統維護中起著異常重要的作用,就好像磨刀不誤砍柴工一樣,日誌就像對系統進行分析的工具,工具便捷了,對系統分析起來就能達到事半功倍的效果。開發者必須要明白日誌的價值和意義,萬萬不可忽略和輕視。 LogCook是一款非常... ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...