自定义Tabbar方法二
来源:互联网 发布:阿里云 oss 图片处理 编辑:程序博客网 时间:2024/09/21 09:06
方法一 隐藏式
原理:将其tabbar即将要出现的时候,将其隐藏,自己写一个UIView类,上面放UIButton类,方法比较麻烦,适合于<IOS 5的,从IOS 6开始后,有了一个新的方法,用于设置其背景图片和选中的图片
代码
.h头文件
#import <UIKit/UIKit.h>
@interface CustomTabBarController :UITabBarController
{
//用于保存上次处于选中的tabbaritem
UIImageView * imagView;
UILabel * lab;
}
@property (nonatomic,retain)NSMutableArray * tabBarImageNames;
@property (nonatomic,retain)NSMutableArray * tabBarSelectedImageNames;
@property (nonatomic,retain)NSMutableArray * tabBarTitleNames;
@property (nonatomic,retain)NSMutableArray * bnts;
@property (nonatomic,retain)UIView * tabBarBackgroundView;
@end
.m文件
#import "CustomTabBarController.h"
#define btnWidth kScreenWidth/self.viewControllers.count
#define btnHeight self.tabBar.frame.size.height
@interface CustomTabBarController ()
@end
@implementation CustomTabBarController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//当程序刚启动时隐藏tabBar
[self.tabBarsetHidden:YES];
}
return self;
}
//初始化自定义的tabBar
- (void)viewWillAppear:(BOOL)animated
{
self.tabBarImageNames = [[NSMutableArrayalloc]initWithObjects:
@"图标1.png",
@"图标2.png",
@"图标3.png",
@"图标4.png",
nil];
self.tabBarSelectedImageNames = [[NSMutableArrayalloc]initWithObjects:
@"图标11.png",
@"图标12.png",
@"图标13.png",
@"图标14.png",
nil];
self.tabBarTitleNames = [[NSMutableArrayalloc]initWithObjects:
@"我的收藏",
@"当前位置",
@"自选位置",
@"更多",
nil];
//初始化自定义的tabbar
[selfinitCustomTabBar];
}
-(void)initCustomTabBar
{
//初始化数据
self.bnts = [[NSMutableArrayalloc]initWithCapacity:self.viewControllers.count];
//tabBar背景视图
UIColor * tabBarBackgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"标题栏背景"]];
self.tabBarBackgroundView = [[UIViewalloc]initWithFrame:self.tabBar.frame];//1
self.tabBarBackgroundView.backgroundColor = tabBarBackgroundColor;
[self.viewinsertSubview:self.tabBarBackgroundViewatIndex:1];
//添加btns
int btnCount = self.viewControllers.count >5 ? 5 : self.viewControllers.count;
for (int i =0 ; i < btnCount; i++) {
//imageView
NSString * imageName = [self.tabBarImageNamesobjectAtIndex:i];
UIImageView * imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,btnWidth,btnHeight)];
imageView.tag =1;
imageView.image = [UIImageimageNamed:imageName];
//label
NSString * imageTitle = [self.tabBarTitleNamesobjectAtIndex:i];
UILabel * label = [[UILabelalloc]initWithFrame:CGRectMake(0,btnHeight-22,btnWidth, btnHeight-22)];
label.tag =2;
label.backgroundColor = [UIColorclearColor];
label.textColor = [UIColorcolorWithRed:77/255.0
green:36/255.0
blue:21/255.0
alpha:1.0];
[label setFont:[UIFontsystemFontOfSize:12.0]];
[label setTextAlignment:UITextAlignmentCenter];
[labelsetText:imageTitle];
UIButton * btnTemp = [UIButtonbuttonWithType:UIButtonTypeCustom];
btnTemp.frame =CGRectMake(i*btnWidth,0, btnWidth,btnHeight);
btnTemp.tag = i;
[btnTemp addTarget:selfaction:@selector(selectedTabbarItem:)forControlEvents:UIControlEventTouchUpInside];
[btnTempaddSubview:imageView];
[btnTempaddSubview:label];
[imageViewrelease];
[labelrelease];
[self.tabBarBackgroundViewaddSubview:btnTemp];
[self.bntsaddObject:btnTemp];
}
[selfselectedTabbarItem:[self.bntsobjectAtIndex:self.selectedIndex]];
}
- (void)selectedTabbarItem:(UIButton *)button
{
//转到相应的视图
self.selectedIndex = button.tag;
//关键步骤如果imagView和lab有值,将其移除
if ([imagViewsuperview]) {
[imagViewremoveFromSuperview];
}
if ([lab superview]) {
[labremoveFromSuperview];
}
//imageView
NSString * imageName = [self.tabBarSelectedImageNamesobjectAtIndex:button.tag];
imagView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,btnWidth,btnHeight)];
imagView.image = [UIImageimageNamed:imageName];
//label
NSString * imageTitle = [self.tabBarTitleNamesobjectAtIndex:button.tag];
lab = [[UILabelalloc]initWithFrame:CGRectMake(0,btnHeight-22,btnWidth, btnHeight-22)];
lab.backgroundColor = [UIColorclearColor];
lab.textColor = [UIColorwhiteColor];
[labsetFont:[UIFontsystemFontOfSize:12.0]];
[labsetTextAlignment:UITextAlignmentCenter];
[labsetText:imageTitle];
[imagView addSubview:lab];
[buttonaddSubview:imagView];
}
- 自定义Tabbar方法二
- 自定义Tabbar方法—
- 自定义tabBar的一个方法
- 非自定义tabbar显示全图片方法
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- 自定义tabbar
- 自定义 TabBar
- 自定义TabBar
- 自定义tabBar
- 自定义TabBar
- 自定义TabBar
- 自定义Tabbar
- 自定义tabbar
- 自定义 TabBar
- 自定义TabBar
- C指针和堆空间 [李园7舍_404]
- LoadRunner:关联HTTP请求
- 基于Java分布技术的消息服务器的设…
- 面向对象设计模式的核心法则
- Cordys服务器启停脚本
- 自定义Tabbar方法二
- android 录音
- 采用HTML替换XFORM技术方案概述
- 基于Cordys平台上的待办任务优化改造方案
- objective-c中Category类别(扩展类)专题总结
- SOA技术架构下时序图的一种表达方式
- Cordys平台开发考虑到的技术点
- 异步加载js方案
- 基于Cordys平台的SSO方案