完全自定义 TabBar
//
// CustomTabBarController.h
// Dream
//
// Created by mac on 14-10-17.
// Copyright (c) 2014年 HM. All rights reserved.
// //标记一下方向
typedef enum
{
ZYSlideDirectionRight = ,
ZYSlideDirectionLeft } ZYSlideDirection; #import <UIKit/UIKit.h> @interface CustomTabBarController : UIViewController<UINavigationControllerDelegate>
{
UIView *_myTabBarView;
}
@property (retain, nonatomic) NSArray *viewControllers;
@property (retain, nonatomic) NSArray *previousNavViewController;
@property (retain, nonatomic) NSArray *buttonArray;
@property (retain, nonatomic) NSArray *normalImageArray;
@property (retain, nonatomic) NSArray *highLightImageArray;
@property (retain, nonatomic) NSArray *nameArray;
@property (assign, nonatomic) int selectedIndex; - (instancetype)initWithNormalImage:(NSArray *)normalImageArray highLightImage:(NSArray *)highLightImageArray BtnName:(NSArray *)nameArray; - (void)tabBarButtonClick:(id)sender; - (void)showTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated;
- (void)hideTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated; @end
//
// CustomTabBarController.m
// Dream
//
// Created by mac on 14-10-17.
// Copyright (c) 2014年 HM. All rights reserved.
// //动画持续时间,该时间与压栈和出栈时间相当
#define SLIDE_ANIMATION_DURATION 0.33 #import "CustomTabBarController.h"
#import "Header.h" @interface CustomTabBarController ()
@property (retain, nonatomic) NSArray *labArray;
@property (retain, nonatomic) NSArray *imageArray;
@end @implementation CustomTabBarController - (void)dealloc
{
[_myTabBarView release];
self.viewControllers = nil;
self.normalImageArray = nil;
self.highLightImageArray = nil;
self.nameArray = nil;
self.previousNavViewController = nil;
self.buttonArray = nil;
self.labArray = nil;
self.imageArray = nil;
[super dealloc];
} - (instancetype)initWithNormalImage:(NSArray *)normalImageArray highLightImage:(NSArray *)highLightImageArray BtnName:(NSArray *)nameArray
{
if (self = [super init])
{
_selectedIndex = -;
self.normalImageArray = normalImageArray;
self.highLightImageArray = highLightImageArray;
self.nameArray = nameArray; }
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; _myTabBarView = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
[self.view addSubview:_myTabBarView]; UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"adv_bottom.png"]];
image.frame = CGRectMake(, , , );
[_myTabBarView addSubview:image];
[image release]; NSMutableArray *arrayB = [NSMutableArray array];
NSMutableArray *arrayI = [NSMutableArray array];
NSMutableArray *arrayL = [NSMutableArray array]; for (int i = ; i < _nameArray.count; i ++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(+*i, , , );
btn.tag = i;
// [btn setImage:[UIImage imageNamed:_normalImageArray[i]] forState:UIControlStateNormal];
// [btn setImage:[UIImage imageNamed:_highLightImageArray[i]] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[_myTabBarView addSubview:btn];
[arrayB addObject:btn]; UIImageView *imageView= [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
imageView.image = [UIImage imageNamed:_normalImageArray[i]];
imageView.highlightedImage = [UIImage imageNamed:_highLightImageArray[i]];
[btn addSubview:imageView]; [arrayI addObject:imageView];
[imageView release]; UILabel *lab = [[[UILabel alloc]init] autorelease];
lab.text = _nameArray[i];
lab.textAlignment = NSTextAlignmentCenter;
lab.font = [UIFont systemFontOfSize:];
lab.frame = CGRectMake(, , , );
[btn addSubview:lab]; [arrayL addObject:lab];
[lab release];
}
self.buttonArray = arrayB;
self.imageArray = arrayI;
self.labArray = arrayL; if (_selectedIndex == -)
{
self.selectedIndex = ;
}
else
{
self.selectedIndex = _selectedIndex;
} } - (void)setSelectedIndex:(int)selectedIndex
{
//如果索引值没有改变不做其他操作
if (_selectedIndex == selectedIndex) return; if (_selectedIndex >= )
{
//找出对应索引的视图控制器
UIViewController *priviousViewController = [_viewControllers objectAtIndex:_selectedIndex];
//移除掉
[priviousViewController.view removeFromSuperview]; UIButton *btn = _buttonArray[_selectedIndex];
btn.selected = NO; UILabel *lab = _labArray[_selectedIndex];
lab.textColor = [UIColor blackColor]; UIImageView *image = _imageArray[_selectedIndex];
image.image = [UIImage imageNamed:_normalImageArray[_selectedIndex]]; } //记录一下当前的索引
_selectedIndex = selectedIndex; //获得对应的按钮并且设置为高亮状态下的图片
UIButton *currentButton = _buttonArray[_selectedIndex];
currentButton.selected = YES;
UILabel *lab = _labArray[_selectedIndex];
lab.textColor = PINK(0.6);
UIImageView *image = _imageArray[_selectedIndex];
image.image = [UIImage imageNamed:_highLightImageArray[_selectedIndex]]; //获得对应的视图控制器
UIViewController *currentViewController = [_viewControllers objectAtIndex:_selectedIndex]; //如果此条件成立表示当前是第一个,即“导航控制器”
if ([currentViewController isKindOfClass:[UINavigationController class]])
{
//设置导航控制器的代理
((UINavigationController *)currentViewController).delegate = self;
}
//设置当前视图的大小
currentViewController.view.frame = CGRectMake(, , , self.view.bounds.size.height - ); //添加到Tab上
[self.view addSubview:currentViewController.view]; //把视图放到TabBar下面
[self.view sendSubviewToBack:currentViewController.view]; } - (void)tabBarButtonClick:(id)sender
{
//获得索引
UIButton *btn = (UIButton *)sender;
int index = (int)btn.tag; //用self.赋值默认会调set方法
self.selectedIndex = index;
} #pragma mark -
#pragma mark UINavigationControllerDelegate - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// NSLog(@"vc is %@",viewController);
/*
第一次加载根视图:previousNavViewController当前导航控制器里面的视图控制器数组
之后显示视图:previousNavViewController操作前的视图控制器数组
*/
if (!_previousNavViewController)
{
//导航控制器中的视图数组
self.previousNavViewController = navigationController.viewControllers;
} /*
是否为压栈的标记,初始化为NO
如果原来的控制器数不大于当前导航的视图控制器数表示是压栈
*/
BOOL isPush = NO; if ([_previousNavViewController count] <= [navigationController.viewControllers count])
{
isPush = YES;
} /*
上一个视图控制器当压栈的时候底部条是否隐藏
当前视图控制器当压栈的时候底部条是否隐藏
这两个视图控制器有可能是同一个
*/
BOOL isPreviousHidden = [[_previousNavViewController lastObject] hidesBottomBarWhenPushed]; BOOL isCurrentHidden = viewController.hidesBottomBarWhenPushed;
// [[navigationController.viewControllers lastObject] hidesBottomBarWhenPushed] //重新记录当前导航器中的视图控制器数组
self.previousNavViewController = navigationController.viewControllers; /*
如果状态相同不做其他操作
如果上一个显示NO,这个隐藏YES,则隐藏TabBar
如果上一个隐藏YES,这个显示NO,则显示TabBar
*/
if (!isPreviousHidden && !isCurrentHidden)
{
return;
}
else if(isPreviousHidden && isCurrentHidden)
{
return;
}
else if(!isPreviousHidden && isCurrentHidden)
{
//隐藏tabbar 压栈
[self hideTabBar:isPush ? ZYSlideDirectionLeft : ZYSlideDirectionRight animated:animated];
}
else if(isPreviousHidden && !isCurrentHidden)
{
//显示tabbar 出栈
[self showTabBar:isPush ? ZYSlideDirectionLeft : ZYSlideDirectionRight animated:animated];
} } /*
显示底部TabBar相关
需要重置当前视图控制器View的高度为整个屏幕的高度-TabBar的高度
*/
- (void)showTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated
{
//根据压栈还是出栈设置TabBar初始位置
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = self.view.bounds.size.width * ( (direction == ZYSlideDirectionRight) ? - : );
_myTabBarView.frame = tempRect; //执行动画
[UIView animateWithDuration:isAnimated ? SLIDE_ANIMATION_DURATION : delay: options: animations:^
{
//动画效果
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = ;
_myTabBarView.frame = tempRect; }
completion:^(BOOL finished)
{
//动画结束时
//重置当前视图控制器View的高度为整个屏幕的高度-TabBar的高度
UIViewController *currentViewController = [_viewControllers objectAtIndex:_selectedIndex]; CGRect viewRect = currentViewController.view.frame;
viewRect.size.height = self.view.bounds.size.height - ;
currentViewController.view.frame = viewRect;
}];
} /*
隐藏底部TabBar相关
需要重置当前视图控制器View的高度为整个屏幕的高度
*/
- (void)hideTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated
{
//获得当前视图控制器
UIViewController *currentViewController = [_viewControllers objectAtIndex:_selectedIndex];
//重置高度
CGRect viewRect = currentViewController.view.frame;
viewRect.size.height = self.view.bounds.size.height;
currentViewController.view.frame = viewRect; //设置TabBar的位置
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = ;
_myTabBarView.frame = tempRect; //采用Block的形式开启一个动画
[UIView animateWithDuration:isAnimated ? SLIDE_ANIMATION_DURATION : delay: options: animations:^(void)
{
//根据压栈还是出栈设置动画效果
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = self.view.bounds.size.width * (direction == ZYSlideDirectionLeft ? - : );
_myTabBarView.frame = tempRect; }
completion:^(BOOL finished){}
];
} @end
完全自定义 TabBar的更多相关文章
- 自定义tabBar
★★★★自定义tabBar★★★★★★★ Demo下载地址:https://github.com/marlonxlj/tabBarCustom.git 前言: 有的时候需求要对tabBar进行自定义的 ...
- IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)
********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...
- iOS 隐藏自定义tabbar
iOS 隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...
- iOS开发之功能模块--关于自定义TabBar条
只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...
- iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]
自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...
- 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究
关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...
- 第二篇、Swift_自定义 tabbar 的 badgeValue显示样式
在实际的开发中,我们常常需要根据实际的需求,去改变bageValue的显示样式,默认是红色的背景,白色的字体颜色 使用方式: class BKTabBarController: UITabBarCon ...
- [iOS微博项目 - 1.6] - 自定义TabBar
A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...
- 1行代码为每个Controller自定义“TabBar”-b
这篇文章大致会带你实现以下的功能,废话少说,先看东西: JPNavigationController.gif Q&A:Demo里都有那些东西? 01.关于自定义导航栏 01.第一个控制器的导航 ...
- iOS 轻松实现自定义TabBar
自定义TabBar的案例网上不少,昨天受到开发小伙伴的影响,尝试了一下非大神的取巧思路:Demo 1.创建RootViewController,后面创建几个继承的VC,将这几个VC添加到TabBarC ...
随机推荐
- 基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)
1.冒泡排序: (1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. (2)外面再套个循环就行. 算法复杂度:O(N2) 不罗嗦,上代码: //冒泡排序(两两交换,外加一个外循环) pub ...
- Todolist
UValive 6041(KD tree) UValive 6042(DP) UValive 6044(图论)
- CodeForces 682B Alyona and Mex (排序+离散化)
Alyona and Mex 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/B Description Someone gave ...
- ALAsset,ALAssetsLibrary,ALAssetsgroup常见属性及用法
转载自 http://www.cnblogs.com/javawebsoa/archive/2013/07/19/3201246.html ALAssetsgroup --------------- ...
- UVa 10900 So you want to be a 2n-aire? (概率DP,数学)
题意:一 个答题赢奖金的问题,玩家初始的金额为1,给出n,表示有n道题目,t表示说答对一道题目的概率在t到1之间,每次面对一道题,可以选择结束游戏, 获得当 前奖金:回答下一道问题,答对的概率p在t到 ...
- WordPress主题制作教程[壹] - 了解WP&结构&索引
最近开始筹备WordPress主题开发了.首先我们在此章节中进行了解什么是WP,以及WP的结构.通过这个文章索引到以后所写的WP系列教程. (抱歉,大家不要急,持续更新中....) 1.首先,我们来认 ...
- VS2015中DataGridView的DataGridViewComBoboxCell列值无效及数据绑定错误的解决方法
在VS2015中练习DataGridView的使用, 发现其中的DataGridViewComBoboxCell列存在着绑定数据库列后出现值无效的提示 根据网上的解决办法,添加了DataError后可 ...
- CSS 边框的宽度
边框的宽度 您可以通过 border-width 属性为边框指定宽度. 为边框指定宽度有两种方法:可以指定长度值,比如 2px 或 0.1em:或者使用 3 个关键字之一,它们分别是 thin .me ...
- 深入Mysql 导入导出
mysql常用导出数据命令:1.mysql导出整个数据库 mysqldump -hhostname -uusername -ppassword databasename > backupfil ...
- cocos2d-x 获取系统时间
转自:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long getCurren ...