TabBarContorller

常規(guī)項(xiàng)目基本上都是一個(gè)TabBarController,下面有三四個(gè)模塊,每個(gè)模塊都是一個(gè)NavigationController。別致一點(diǎn)的會(huì)在tabBar中間自定義一個(gè)樣式,如 “ + ” 號(hào)等。
系統(tǒng)的UITabBarController自然是可以,但還是不盡如人意。所以項(xiàng)目中多是下面兩個(gè)第三方TabBarController.

CYLTabBarController

#import "HomeViewController.h"

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "WPNavigationController.h"

@interface HomeViewController ()<UITabBarDelegate>

@end

@implementation HomeViewController
//初始化
- (instancetype)init{
    self = [super init];
    if (self) {
        //設(shè)置控制器
        [self setupTabBarController];
        //設(shè)置tabBar 高度
        self.tabBarHeight = 60;
        self.tabBarController.tabBar.delegate = self;
        self.moreNavigationController.navigationBarHidden = NO;
    }
    return self;
}
//設(shè)置子視圖
- (void)setupTabBarController{
    
    FirstViewController *firstVC = [[FirstViewController alloc]init];
    firstVC.view.backgroundColor = [UIColor whiteColor];
    WPNavigationController *firstNav = [[WPNavigationController alloc]initWithRootViewController:firstVC];
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    secondVC.view.backgroundColor = [UIColor greenColor];
    WPNavigationController *secondNav = [[WPNavigationController alloc]initWithRootViewController:secondVC];
    
    
    NSDictionary *dic1 = @{
                           CYLTabBarItemTitle:@"首頁",
                           CYLTabBarItemImage:@"home_unselected",
                           CYLTabBarItemSelectedImage:@"home_selected",
                           
                           };
    NSDictionary *dic2 = @{
                           CYLTabBarItemTitle:@"我的",
                           CYLTabBarItemImage:@"me_unselected",
                           CYLTabBarItemSelectedImage:@"me_selected",
                           };
    self.tabBarItemsAttributes = @[dic1,dic2];

    //最后一步設(shè)置viewControllers
    [self setViewControllers:@[firstNav,secondNav]];
    //詳細(xì)設(shè)置tabBar的樣式
    [self customizeTabBarAppearance:self];
}
/**
 *  可以詳細(xì)設(shè)置tabBar的樣式內(nèi)容
 */
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
    
    // 普通狀態(tài)下的文字屬性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = HEXCOLOR(0x2c2c2c);
    
    // 選中狀態(tài)下的文字屬性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = HEXCOLOR(0xd81e06);
    
    // 設(shè)置文字屬性
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    [tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    // 設(shè)置背景圖片
    UITabBar *tabBar = [UITabBar appearance];
    [tabBar setBarStyle:UIBarStyleDefault];     //設(shè)置tabBar樣式
    [tabBar setBarTintColor:[UIColor whiteColor]];//設(shè)置tabBar背景色
    // 去除 TabBar 自帶的頂部陰影
    [tabBar setShadowImage:[[UIImage alloc] init]];    
    
}

#pragma mark - Delegate Method
#pragma mark - UITabBarControllerDelegate Method
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    if ([item.title isEqualToString:@"我的"]) {
        NSLog(@"點(diǎn)擊了我的");
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

CYLTabBarController 的父類是UITabBarController,所以可以正常使用父類的所有屬性和方法,另外,它最牛X的地方在于自定義特殊tabBarItem時(shí)的低耦合性。
只要在工程中新建一個(gè)繼承自CYLPlusButton 的類,并實(shí)現(xiàn)相應(yīng)的代理方法,會(huì)自動(dòng)檢測(cè)生成。

#import "CYLPlusButtonSubclass.h"
#import "CYLTabBarController.h"

@interface CYLPlusButtonSubclass ()< CYLPlusButtonSubclassing >
{
    CGFloat _buttonImageHeight;
}
@end

@implementation CYLPlusButtonSubclass

#pragma mark - 注冊(cè)
+ (void)load {
    [super registerPlusButton];
}

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

#pragma mark上下結(jié)構(gòu)的 button
- (void)layoutSubviews {//initWithFrame時(shí)會(huì)觸發(fā)
    
    [super layoutSubviews];
    
    // 控件大小,間距大小.
    //設(shè)置圖片大小 Width = Height
    CGFloat const imageViewEdgeWidth   = self.bounds.size.width * 0.6;
    CGFloat const imageViewEdgeHeight  = imageViewEdgeWidth ;
    
    CGFloat const centerOfView    = self.bounds.size.width * 0.5; //X軸中心點(diǎn)
    
    CGFloat const labelLineHeight = self.titleLabel.font.lineHeight; //行高
    
    // imageView 和 titleLabel 中心的 Y 值
    CGFloat const centerOfImageView  =  imageViewEdgeWidth * 0.5;
    CGFloat const centerOfTitleLabel = imageViewEdgeHeight  + 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 - CYLPlusButtonSubclassing Methods

//奇數(shù)時(shí)可以不用實(shí)現(xiàn)
+ (NSUInteger)indexOfPlusButtonInTabBar {
    return 1;
}
/**
 *  帶標(biāo)題
 */
+ (id)plusButton {
    
 CYLPlusButtonSubclass *button = [[CYLPlusButtonSubclass alloc] init];
    UIImage *buttonImage_unSelected = [UIImage imageNamed:@"center_unselected"];
    UIImage *buttonImage_selected = [UIImage imageNamed:@"center"];
    [button setImage:buttonImage_selected forState:UIControlStateSelected];
    [button setImage:buttonImage_unSelected forState:UIControlStateNormal];
    [button setTitle:@"思考" forState:UIControlStateNormal];
    [button setTitleColor:HEXCOLOR(0x2c2c2c) forState:UIControlStateNormal];
    [button setTitleColor:HEXCOLOR(0xd81e06) forState:UIControlStateSelected];
    
    button.titleLabel.font = [UIFont systemFontOfSize:11];
    [button sizeToFit];
   
    //[button addTarget:button action:@selector(clickPublisha) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
/**
 *  兩個(gè)可選的代理方法都在設(shè)置button的中心點(diǎn)Y軸方向上的位置
 *  PlusButtonCenterY = multiplierOfTabBarHeight * taBarHeight + constantOfPlusButtonCenterYOffset;
 */

/**
 *  返回值大于0.5表示偏下
 *  返回值小于0.5表示偏上
 *  在圖片大小超出tabBar高度時(shí)實(shí)現(xiàn)這個(gè)方法,可以精細(xì)控制
 */
+ (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight {
    return  0.6;
}
/**
 *  返回值大于0會(huì)向下偏移
 *  返回值小于0會(huì)向上偏移
 */
+ (CGFloat)constantOfPlusButtonCenterYOffsetForTabBarHeight:(CGFloat)tabBarHeight {
    return  -20;
}

/*
 *  不帶標(biāo)題
 */
//+ (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;
//}

實(shí)現(xiàn)下面這個(gè)方法后,自定義的item 會(huì)跟其它item一樣點(diǎn)擊后進(jìn)入新的VC,如果不實(shí)現(xiàn)相當(dāng)于一個(gè)特殊的button,并且一定要實(shí)現(xiàn)indexOfPlusButtonInTabBar。

+ (UIViewController *)plusChildViewController {
    UIViewController *plusChildViewController = [[UIViewController alloc] init];
    plusChildViewController.view.backgroundColor = [UIColor redColor];
    plusChildViewController.navigationItem.title = @"PlusChildViewController";
    UIViewController *plusChildNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:plusChildViewController];
    return plusChildNavigationController;
}
- (void)clickPublisha{
    NSLog(@"gggg");
}

RDVTabBarController

RDVTabBarController 的父類是UIViewController,所以無法直接使用UITabBarController的方法,圖標(biāo)也無法很方便的自定義。

@interface MainTabViewController()<RDVTabBarControllerDelegate>
@end

@implementation SMTMainTabViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate = self;
    [self setUpControllers];
}
-(void)setUpControllers
{
    FirstViewController *firstVC = [[FirstViewController alloc]init];
    firstVC.view.backgroundColor = [UIColor whiteColor];
    WPNavigationController *firstNav = [[WPNavigationController alloc]initWithRootViewController:firstVC];
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    secondVC.view.backgroundColor = [UIColor greenColor];
    WPNavigationController *secondNav = [[WPNavigationController alloc]initWithRootViewController:secondVC];

    
    [self setViewControllers:@[firstNav, secondNav]];
    
    //定制tabbar
    [self customizeTabBar];
}
- (void)customizeTabBar
{
    UIImage *imgSelected = nil;
    UIImage *imgUnselected = nil;
    NSString*title = @"";
    
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[self tabBar] items]){
        if (index == 0){
            imgSelected = [UIImage imageNamed:@"首頁"];
            imgUnselected = [UIImage imageNamed:@"un首頁"];
            title = @"首頁";
        } else if(index == 1){
            imgSelected = [UIImage imageNamed:@"我的"];
            imgUnselected = [UIImage imageNamed:@"un我的"];
            title = @"我的";
        }
        item.title = title;

        //調(diào)整item 圖標(biāo)和標(biāo)題的位置,使之平排
       //item.titlePositionAdjustment = UIOffsetMake(20, -11);
       //item.imagePositionAdjustment = UIOffsetMake(0, 3);
        
        [item setFinishedSelectedImage:imgSelected withFinishedUnselectedImage:imgUnselected];
        index++;
    }
}    
#pragma mark - RDVTabBarControllerDelegate Methods
- (BOOL)tabBarController:(RDVTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    return YES;
}

PS:VC中要得到RDVTabBarController :
self.rdv_tabBarItem;
self.rdv_tabBarController

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,202評(píng)論 4 61
  • 代碼創(chuàng)建UIWindow對(duì)象 Xcode7之后使用代碼創(chuàng)建UIWindow對(duì)象: //創(chuàng)建UIWindow對(duì)象 s...
    云之君兮鵬閱讀 1,360評(píng)論 0 2
  • 有了框圖,我們就可以以一種直觀的方式來表示程序,也可以用它來在設(shè)計(jì)時(shí)理清思路,在有了問題的時(shí)候幫助查漏補(bǔ)缺……所以...
    過客閱讀 332評(píng)論 0 0
  • 多年前看了《異類》這本書,主要想研究研究比爾蓋茨的成功方法 哦原來首富的成功是因?yàn)楦赣H是大律師母親是銀行家千金 I...
    智智楊閱讀 837評(píng)論 3 4
  • 今天走進(jìn)班有幾個(gè)女生在抽噎,眼圈紅紅的,我以為是第一場(chǎng)考試沒考好!所以我便走近勸她們,結(jié)果越勸哭得越痛,有個(gè)快嘴男...
    楊仁珮001閱讀 712評(píng)論 0 0