自定义tabBar(适用于有选中图片随着移动的情况)

来源:互联网 发布:js在线格式化 编辑:程序博客网 时间:2024/06/02 16:57

#import "RootTabBarController.h"#import "HomeViewController.h"#import "NewsViewController.h"#import "TopViewController.h"#import "MoreViewController.h"#import "MovieViewController.h"#define screenW [UIScreen mainScreen].bounds.size.width#define nameLableTextFont [UIFont systemFontOfSize:10]@interface RootTabBarController (){    UIImageView *_selectImg;}@end@implementation RootTabBarController- (void)viewDidLoad {    [super viewDidLoad];    //1. 创建视图    [self creatVC];       //2. 自定义标签栏    [self creatTabBar];      }//1. 创建控制器- (void)creatVC{    //1. 创建视图控制器    HomeViewController *firstCtrl = [[HomeViewController alloc] init];    NewsViewController *secondCtrl = [[NewsViewController alloc] init];    TopViewController *thirdCtrl = [[TopViewController alloc] init];    MovieViewController *forthCtrl = [[MovieViewController alloc] init];    MoreViewController *fifthCtrl = [[MoreViewController alloc] init];             NSArray *viewCtrls = @[firstCtrl,secondCtrl,thirdCtrl,forthCtrl,fifthCtrl];       //2. 创建导航控制器    NSMutableArray *navCtrls = [[NSMutableArray alloc] init];       //导航栏标题    NSArray *navigationTitle = @[@"北美榜", @"新闻", @"TOP250", @"影院", @"更多"];       for (int i=0; i<viewCtrls.count; i++) {              //2.1 取得视图控制器        UIViewController *viewCtrl = viewCtrls[i];               UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];               //2.2.设置导航栏背景图片        [navCtrl.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg_all-64@2x"] forBarMetrics:UIBarMetricsDefault];               //1.设置导航栏标题        viewCtrl.title = navigationTitle[i];        NSDictionary *dic = @{                              NSFontAttributeName:[UIFont systemFontOfSize:18],                              NSForegroundColorAttributeName:[UIColor whiteColor]                              };        [viewCtrl.navigationController.navigationBar setTitleTextAttributes:dic];                      //2.2 将导航控制器存入数组        [navCtrls addObject:navCtrl];    }       //3.交给标签控制器管理    self.viewControllers = navCtrls;      }//2. 自定义标签栏- (void)creatTabBar{    //1. 取得标签栏上得子空间    NSArray *array = self.tabBar.subviews;       //2. 循环删除子控件    for (UIView *sub in array) {              //2.1 判断是否属于UITabBarButton这个类       Class class = NSClassFromString(@"UITabBarButton");               //2.2 如果属于这个类就删除        if ([sub isKindOfClass:class]) {                       [sub removeFromSuperview];        }    }       //3. 设置tabBar背景图片    self.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bg_all"];       //4. 添加按钮    //4.1 定义按钮图像和文字数组    NSArray *btnImageArray = @[@"movie_home", @"msg_new", @"start_top250", @"icon_cinema", @"more_setting"];    NSArray *nameLableArray = @[@"首页", @"新闻", @"TOP", @"电影", @"更多"];       CGFloat width = screenW / btnImageArray.count;       //5. 循环添加按钮    for (int i = 0; i < btnImageArray.count; i++) {       //        5.1 添加tabBar最底层view        CGFloat backViewW = 45;        CGFloat backViewH = self.tabBar.bounds.size.height;        CGFloat backViewX = (width-40) * 0.5 + width * i;        CGFloat backViewY = 0;              UIView *backView = [[UIView alloc] init];        backView.frame = CGRectMake(backViewX, backViewY, backViewW, backViewH);              //        5.2 添加按钮图片        CGFloat tabBarItemImageW = 20;        CGFloat tabBarItemImageH = 20;        CGFloat tabBarItemImageX = (backViewW - tabBarItemImageW) * 0.5;        CGFloat tabBarItemImageY = 5;        UIImageView *tabBarItemImage = [[UIImageView alloc] init];        tabBarItemImage.frame = CGRectMake(tabBarItemImageX, tabBarItemImageY, tabBarItemImageW, tabBarItemImageH);              tabBarItemImage.contentMode = UIViewContentModeScaleAspectFit;        tabBarItemImage.image = [UIImage imageNamed:btnImageArray[i]];;        [backView addSubview:tabBarItemImage];       //        5.3 添加lable        CGFloat nameLableX = 0;        CGFloat nameLableY = tabBarItemImageH;        CGFloat nameLableW = backViewW;        CGFloat nameLableH = backViewH - tabBarItemImageH;        UILabel *nameLable = [[UILabel alloc] init];        nameLable.textColor = [UIColor whiteColor];        nameLable.text = nameLableArray[i];        nameLable.textAlignment = NSTextAlignmentCenter;        nameLable.frame = CGRectMake(nameLableX, nameLableY, nameLableW, nameLableH);        [backView addSubview:nameLable];        nameLable.font = nameLableTextFont;     //        5.4 将背景view添加到tabbar        [self.tabBar addSubview:backView];       //        5.5 添加大按钮        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];        btn.frame = backView.bounds;        [backView addSubview:btn];               btn.tag = i;               //添加点击事件        [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    }       //5.添加选中图标    CGFloat imgW = 45;    CGFloat imgH = self.tabBar.bounds.size.height;    CGFloat imgX = (width-40) * 0.5 ;    CGFloat imgY = 0;    _selectImg = [[UIImageView alloc] init];    _selectImg.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];    _selectImg.frame = CGRectMake(imgX, imgY, imgW, imgH);       [self.tabBar addSubview:_selectImg];    [self.tabBar sendSubviewToBack:_selectImg];     }#pragma mark - 按钮点击事件- (void)buttonAction:(UIButton *)button {       //切换视图控制器    NSInteger tag = button.tag;    self.selectedIndex = tag;       [UIView animateWithDuration:.35 animations:^{        //切换_selectImg的位置               _selectImg.center = button.superview.center;           }];}



实现按钮有图片和文字的第二种方式:

1.新建类继承于UIButton2.复写两个方法#pragma mark 设置button内部的image的范围- (CGRect)imageRectForContentRect:(CGRect)contentRect{    CGFloat imageW = contentRect.size.width;    CGFloat imageH = contentRect.size.height * 0.6;        return CGRectMake(0, 5, imageW, imageH);}#pragma mark 设置button内部的title的范围- (CGRect)titleRectForContentRect:(CGRect)contentRect{    CGFloat titleY = contentRect.size.height * 0.6;    CGFloat titleW = contentRect.size.width;    CGFloat titleH = contentRect.size.height - titleY;        return CGRectMake(0, titleY, titleW, titleH);}
 //设置按钮标题字体  button.titleLabel.font = TABBARTITLE;        //设置标题  button setTitle:@"测试"forState:UIControlStateNormal];

//self代表标签控制器

self.selectedIndex = button.tag;






0 0