IOS自定义UITabBarController
2014-04-10 12:19
387 查看
在IOS7之前,Xcode中自带的UITabBarController控制器的样子很难看,
如果我们的项目是粉色系的,在做适配的时候这个颜色UITabBarController和整个项目很不搭配,那么,如何才能自定义一个UITabBarController,样子看起来和IOS7的视觉效果一样呢?!
具体代码如下:
自定义一个基于UITabBarController的类,
.h文件的具体代码
#import <Foundation/Foundation.h>
@interface CustomtabBarController :UITabBarController {
NSMutableArray *buttons;
int currentSelectedIndex;
UIImageView *slideBg;
UIView *cusTomTabBarView;
UIImageView *backGroundImageView;
int viewCount;
}
@property (nonatomic,assign)
int currentSelectedIndex;
@property (nonatomic,retain)
UIColor *tabColor;
@property (nonatomic,retain)
NSArray *iconArray;
@property (nonatomic,retain)NSMutableArray *buttons;
- (void)customTabBar;
-(id)initWithViewCount:(NSInteger)count;
- (void)selectedTab:(UIButton *)button;
@end
.m文件的具体代码
#import "CustomtabBarController.h"
@implementation CustomtabBarController
@synthesize currentSelectedIndex;
@synthesize buttons;
- (void)viewWillAppear:(BOOL)animated{
[selfcustomTabBar];
}
-(id)initWithViewCount:(NSInteger)count{
self = [superinit];
if(self){
viewCount = count;
}
return
self;
}
//自定义tabbar
- (void)customTabBar{
//获取tabbar的frame
CGRect tabBarFrame =self.tabBar.frame;
backGroundImageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(0,0,
320,49)];
cusTomTabBarView = [[UIViewalloc]
initWithFrame:tabBarFrame];
//设置tabbar背景
//backGroundImageView.image = [UIImage imageNamed:@"banner.png"];
[cusTomTabBarViewaddSubview:backGroundImageView];
cusTomTabBarView.backgroundColor =self.tabColor;
//创建按钮
self.buttons = [NSMutableArrayarrayWithCapacity:viewCount];
double _width =320 /
viewCount;
double _height =self.tabBar.frame.size.height;
for (int i =0; i <
viewCount; i++) {
UIViewController *viewController = [self.viewControllersobjectAtIndex:i];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame =CGRectMake(i*_width,
0, _width, _height);
[btn addTarget:selfaction:@selector(selectedTab:)forControlEvents:UIControlEventTouchDown];
btn.tag = i;
[btn setImage:[self.iconArrayobjectAtIndex:i]
forState:UIControlStateNormal];
[btn setImageEdgeInsets:UIEdgeInsetsMake(-10,0,
0,0)];
//添加标题
UILabel *titleLabel = [[UILabelalloc]
initWithFrame:CGRectMake(0, _height-18, _width, _height-30)];
titleLabel.backgroundColor = [UIColorclearColor];
titleLabel.text = viewController.tabBarItem.title;
[titleLabel
setFont:[UIFontsystemFontOfSize:12]];
titleLabel.textAlignment =1;
titleLabel.textColor = [UIColorwhiteColor];
[btnaddSubview:titleLabel];
[self.buttonsaddObject:btn];
//添加按钮之间的分割线,第一个位置和最后一个位置不需要添加
if (i>0 && i<4) {
UIImageView *splitView = [[UIImageViewalloc]
initWithImage:[UIImageimageNamed:@"split"]];
splitView.frame =CGRectMake(i*_width-1,0,splitView.frame.size.width,splitView.frame.size.height);
[cusTomTabBarViewaddSubview:splitView];
}
[cusTomTabBarViewaddSubview:btn];
}
[self.viewaddSubview:cusTomTabBarView];
[cusTomTabBarViewaddSubview:slideBg];
}
//切换tabbar
- (void)selectedTab:(UIButton *)button{
self.currentSelectedIndex = button.tag;
self.selectedIndex =self.currentSelectedIndex;
//[self performSelector:@selector(slideTabBg:) withObject:button];
}
//切换滑块位置
- (void)slideTabBg:(UIButton *)btn{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.20];
[UIViewsetAnimationDelegate:self];
slideBg.frame = btn.frame;
[UIViewcommitAnimations];
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimationanimationWithKeyPath:@"transform"];
animation.duration =0.50;
animation.delegate =self;
animation.removedOnCompletion =YES;
animation.fillMode =kCAFillModeForwards;
NSMutableArray *values = [NSMutableArrayarray];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(0.1,0.1,
1.0)]];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(1.2,1.2,
1.0)]];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(0.9,0.9,
0.9)]];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(1.0,1.0,
1.0)]];
animation.values = values;
[btn.layeraddAnimation:animation
forKey:nil];
}
@end
然后倒入都文件就可以调用这个自定义的UITabBarController
CustomtabBarController *mainTab = [[CustomtabBarControlleralloc]initWithViewCount:5];Count是几个数组
mainTab.iconArray是图片数组
mainTab.tabColor颜色
可以按照自己的要求给相应地几个按钮,图片数组,和颜色就可以了
ios6以下的效果就会和ios7的效果一样的
如果我们的项目是粉色系的,在做适配的时候这个颜色UITabBarController和整个项目很不搭配,那么,如何才能自定义一个UITabBarController,样子看起来和IOS7的视觉效果一样呢?!
具体代码如下:
自定义一个基于UITabBarController的类,
.h文件的具体代码
#import <Foundation/Foundation.h>
@interface CustomtabBarController :UITabBarController {
NSMutableArray *buttons;
int currentSelectedIndex;
UIImageView *slideBg;
UIView *cusTomTabBarView;
UIImageView *backGroundImageView;
int viewCount;
}
@property (nonatomic,assign)
int currentSelectedIndex;
@property (nonatomic,retain)
UIColor *tabColor;
@property (nonatomic,retain)
NSArray *iconArray;
@property (nonatomic,retain)NSMutableArray *buttons;
- (void)customTabBar;
-(id)initWithViewCount:(NSInteger)count;
- (void)selectedTab:(UIButton *)button;
@end
.m文件的具体代码
#import "CustomtabBarController.h"
@implementation CustomtabBarController
@synthesize currentSelectedIndex;
@synthesize buttons;
- (void)viewWillAppear:(BOOL)animated{
[selfcustomTabBar];
}
-(id)initWithViewCount:(NSInteger)count{
self = [superinit];
if(self){
viewCount = count;
}
return
self;
}
//自定义tabbar
- (void)customTabBar{
//获取tabbar的frame
CGRect tabBarFrame =self.tabBar.frame;
backGroundImageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(0,0,
320,49)];
cusTomTabBarView = [[UIViewalloc]
initWithFrame:tabBarFrame];
//设置tabbar背景
//backGroundImageView.image = [UIImage imageNamed:@"banner.png"];
[cusTomTabBarViewaddSubview:backGroundImageView];
cusTomTabBarView.backgroundColor =self.tabColor;
//创建按钮
self.buttons = [NSMutableArrayarrayWithCapacity:viewCount];
double _width =320 /
viewCount;
double _height =self.tabBar.frame.size.height;
for (int i =0; i <
viewCount; i++) {
UIViewController *viewController = [self.viewControllersobjectAtIndex:i];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame =CGRectMake(i*_width,
0, _width, _height);
[btn addTarget:selfaction:@selector(selectedTab:)forControlEvents:UIControlEventTouchDown];
btn.tag = i;
[btn setImage:[self.iconArrayobjectAtIndex:i]
forState:UIControlStateNormal];
[btn setImageEdgeInsets:UIEdgeInsetsMake(-10,0,
0,0)];
//添加标题
UILabel *titleLabel = [[UILabelalloc]
initWithFrame:CGRectMake(0, _height-18, _width, _height-30)];
titleLabel.backgroundColor = [UIColorclearColor];
titleLabel.text = viewController.tabBarItem.title;
[titleLabel
setFont:[UIFontsystemFontOfSize:12]];
titleLabel.textAlignment =1;
titleLabel.textColor = [UIColorwhiteColor];
[btnaddSubview:titleLabel];
[self.buttonsaddObject:btn];
//添加按钮之间的分割线,第一个位置和最后一个位置不需要添加
if (i>0 && i<4) {
UIImageView *splitView = [[UIImageViewalloc]
initWithImage:[UIImageimageNamed:@"split"]];
splitView.frame =CGRectMake(i*_width-1,0,splitView.frame.size.width,splitView.frame.size.height);
[cusTomTabBarViewaddSubview:splitView];
}
[cusTomTabBarViewaddSubview:btn];
}
[self.viewaddSubview:cusTomTabBarView];
[cusTomTabBarViewaddSubview:slideBg];
}
//切换tabbar
- (void)selectedTab:(UIButton *)button{
self.currentSelectedIndex = button.tag;
self.selectedIndex =self.currentSelectedIndex;
//[self performSelector:@selector(slideTabBg:) withObject:button];
}
//切换滑块位置
- (void)slideTabBg:(UIButton *)btn{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.20];
[UIViewsetAnimationDelegate:self];
slideBg.frame = btn.frame;
[UIViewcommitAnimations];
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimationanimationWithKeyPath:@"transform"];
animation.duration =0.50;
animation.delegate =self;
animation.removedOnCompletion =YES;
animation.fillMode =kCAFillModeForwards;
NSMutableArray *values = [NSMutableArrayarray];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(0.1,0.1,
1.0)]];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(1.2,1.2,
1.0)]];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(0.9,0.9,
0.9)]];
[values addObject:[NSValuevalueWithCATransform3D:CATransform3DMakeScale(1.0,1.0,
1.0)]];
animation.values = values;
[btn.layeraddAnimation:animation
forKey:nil];
}
@end
然后倒入都文件就可以调用这个自定义的UITabBarController
CustomtabBarController *mainTab = [[CustomtabBarControlleralloc]initWithViewCount:5];Count是几个数组
mainTab.iconArray是图片数组
mainTab.tabColor颜色
可以按照自己的要求给相应地几个按钮,图片数组,和颜色就可以了
ios6以下的效果就会和ios7的效果一样的
相关文章推荐
- IOS快速自定义UITabBarController内的TabBar控件
- iOS中 UITabBarController中自定义UITabBar
- iOS--自定义UITabBarController
- iOS-快速自定义UITabBarController内的tabbar控件
- iOS中 UITabBarController中自定义UITabBar
- iOS UITabBarController 的 UITabBar 自定义
- IOS 自定义UITabBarController的高度处理
- iOS 自定义UITabBarController的tabBar
- iOS - Swift 自定义UITabBarController
- IOS博客项目搭建-02-自定义底部导航控制器UITabBarController
- IOS开发之自定义UITabBarController
- iOS 自定义UITabBarController以及UITabBar的分析
- iOS 自定义UITabbarController中的item (swift)
- iOS开发:自定义UITabBarController标签控制栏
- iOS--UITabbarController自定义
- iOS 关于自定义转场动画,以UITabBarController为例
- ios开发之自定义UITabBarController
- 从0开始架构一个IOS程序 ——04— UITabBarController和自定义TabBar 搭建主页面
- iOS自定义控件 - 自定义UITabBarController中的TabBar
- IOS总结_无需自定义UITabbar也可改变UITabbarController的背景和点击和的颜色