自定义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
- 自定义tabBar(适用于有选中图片随着移动的情况)
- TabBar自定义图片的设置
- tabbar未选中和选中时设置不同的图片
- 图片随着鼠标移动
- 设置tabbar有被选中的效果,添加背景
- ios中对于tabBar选中图片设置的问题
- ios中对于tabBar选中图片设置的问题
- ios中对于tabBar选中图片设置的问题
- ios中对于tabBar选中图片设置的问题
- 自定义工具栏tabbar图片
- Cell特效之图片随着TableView的滑动上下移动
- 压缩文件(zip)--适用于目录下既有目录又有文件的情况
- UITabBarController 之 设置选中和未选中的图片 隐藏tabbar
- UITabBarController 之 设置选中和未选中的图片 隐藏tabbar
- iOS自定义tabbar(没有tabbar上的黑线)
- tabBar中tabBarItem选中颜色自定义设置
- iOS开发, tabbar自定义图片
- 自定义系统Tabbar选择后的图片和字体颜色
- SQL Server 2012 链接本地服务器失败
- apache虚拟主机配置
- WCF 设计和实现服务协定(01)
- C#面试题整理2
- HDU 2955 01背包
- 自定义tabBar(适用于有选中图片随着移动的情况)
- oracle 10g 各版本下载地址
- vps简单测试命令收藏
- centos 6.5 NVIDIA GPU caffe 配置
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第五讲:无序列表、有序列表、框架
- python脚本语言2
- 利用反射技术实现POJO的数据库操作
- 【Android Studio快捷键】之导入相应包声明
- hdu 3420 Bus Fair