UITabBarController 类似自定义效果的实现

本文详细介绍了如何创建一个自定义的iOS标签栏控制器,包括初始化视图、配置标签栏样式、设置导航控制器和标题等功能。实现了在iOS应用中灵活定制标签栏外观和行为的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//.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.

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值