UITabBarController 類似自定義效果的實現

//.h文件

#import <UIKit/UIKit.h>

@interface MyTabBarController : UITabBarController

@end


// .m文件

#import "MyTabBarController.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "ThirdViewController.h"

#import "FourViewController.h"


// 自定義標籤欄控制器

@interface MyTabBarController ()

@property(nonatomic,strong)FirstViewController * first;

@property(nonatomic,strong)SecondViewController * second;

@property(nonatomic,strong)ThirdViewController * third;

@property(nonatomic,strong) FourViewController * four;


@end


@implementation MyTabBarController


- (void) initView

{

    // 標題

    NSArray * arr_title = [NSArray arrayWithObjects:@"時尚圈",@"賬單",@"我的",@"其他", nil];

    

    // 未選中狀態的image

    NSArray * arr_unselected = [NSArray arrayWithObjects:[UIImage imageNamed:@"item_xuexinbao"],[UIImage imageNamed:@"item_zhangdan"],[UIImage imageNamed:@"item_xinyong"],[UIImage imageNamed:@"item_account"], nil];


    // 選中狀態的image

  //  UIImage * image = [[UIImage  imageNamed:@"item_xuexinbao_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// ios7新特性 image的渲染效果(三種:1.根據上下文環境決定image的顏色 2.保持image原有的顏色 3.根據tintColor決定image的顏色)


// 使用這個新特性 讓每一個item的選中狀態的圖片保留原有的顏色

    NSArray * arr_selected = [NSArray arrayWithObjects:[[UIImage  imageNamed:@"item_xuexinbao_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ],[[UIImage imageNamed:@"item_zhangdan_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal],[[UIImage imageNamed:@"item_xinyong_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal],[[UIImage imageNamed:@"item_account_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal], nil];

    _first = [[FirstViewController alloc] init];

    _first.title = [arr_title objectAtIndex:0];

    UINavigationController * firstNC = [[UINavigationController alloc] initWithRootViewController:_first];

    

    _second = [[SecondViewController alloc] init];

    _second.title = [arr_title objectAtIndex:1];

    UINavigationController * secondNC = [[UINavigationController alloc] initWithRootViewController:_second];

    

    _third = [[ThirdViewController alloc] init];

    _third.title = [arr_title objectAtIndex:2];

    UINavigationController * thirdNC = [[UINavigationController alloc] initWithRootViewController:_third];

    

    _four = [[FourViewController alloc] init];

    _four.title = [arr_title objectAtIndex:3];

    UINavigationController * fourNC = [[UINavigationController alloc] initWithRootViewController:_four];

    

    if (!ios7) {

        // 如果是ios7.0 以下的版本 設置標題和圖片

        _first.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:0] image:nil tag:0];

        [_first.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:0] withFinishedUnselectedImage:[arr_unselected objectAtIndex:0]];

        

        _second.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:1] image:nil tag:1];

        [_second.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:1] withFinishedUnselectedImage:[arr_unselected objectAtIndex:1]];

        

        _third.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:2] image:nil tag:2];

        [_third.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:2] withFinishedUnselectedImage:[arr_unselected objectAtIndex:2]];

        

        _four.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:3] image:nil tag:3];

        [_four.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:3] withFinishedUnselectedImage:[arr_unselected objectAtIndex:3]];

        

    }else{

     // 如果是ios7.0 以上的版本 設置標題和圖片

        _first.tabBarItem  = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:0] image:[arr_unselected objectAtIndex:0] selectedImage:[arr_selected objectAtIndex:0]];

        

        _second.tabBarItem  = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:1] image:[arr_unselected objectAtIndex:1] selectedImage:[arr_selected objectAtIndex:1]];

         _third.tabBarItem  = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:2] image:[arr_unselected objectAtIndex:2] selectedImage:[arr_selected objectAtIndex:2]];

        

         _four.tabBarItem  = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:3] image:[arr_unselected objectAtIndex:3] selectedImage:[arr_selected objectAtIndex:3]];     

    }

    // 設置每一個item 選中 非選中 字體顏色和字體大小

    [self unselectedTapTabBarItem:_first.tabBarItem];

    [self selectedTapTabBarItem:_first.tabBarItem];

    

    [self unselectedTapTabBarItem:_second.tabBarItem];

    [self selectedTapTabBarItem:_second.tabBarItem];

    

    [self unselectedTapTabBarItem:_third.tabBarItem];

    [self selectedTapTabBarItem:_third.tabBarItem];

    

    [self unselectedTapTabBarItem:_four.tabBarItem];

    [self selectedTapTabBarItem:_four.tabBarItem];

    

    self.viewControllers = @[firstNC,secondNC,thirdNC,fourNC];

    // 默認顏色 是藍色  要更改顏色

  

}

// 未選中 點擊

- (void)unselectedTapTabBarItem:(UITabBarItem*)item

{

    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:16]} forState:UIControlStateNormal];

}

// 選中 點擊

- (void)selectedTapTabBarItem:(UITabBarItem*)item

{

    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor],NSFontAttributeName:[UIFont systemFontOfSize:16]} forState:UIControlStateSelected];


}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

   self.tabBar.barTintColor = KTabBarItemTintColor;// ios7 新特性 :tintColor

  

    [self initView];

     

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章