您的位置:首页 > 产品设计 > UI/UE

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的效果一样的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息