在某些项目的初期我们经常会选择使用UITabbarController或者是UINavigationController或者是两者的结合,经常需要自定义自己需要的类,本文讲述了自定义UITabbarController及向UITabBar中添加自定义按钮的使用。
首先自定义一个继承自UITabbarController的类,定义4个属性,均为UIViewController类型的,作为tabbarController的子视图,然后分别将他们加入到自定义的UITabbarController的子控制视图。自定义的UITabbarController命名为WWTTabController。写一个方法用来做一些初始化的工作,然后再viewDidLoad里面调用方法如下:
#pragma mark 初始化tabBar对应的ViewControllers
- (void)setControllers{
//配置的各个tabbar对应的controller
self.vc1= [[UIViewControlleralloc]init];
self.vc1.view.backgroundColor= [UIColorredColor];
self.vc1.tabBarItem= [selfitemWithSelectedImage:@"tabBar_me_icon@2x"image:@"tabBar_me_icon@2x"title:@"首页"];
//配置的各个tabbar对应的controller
self.vc2= [[UIViewControlleralloc]init];
self.vc2.view.backgroundColor= [UIColorblueColor];
self.vc2.tabBarItem= [selfitemWithSelectedImage:@"tabBar_me_icon@2x"image:@"tabBar_me_icon@2x"title:@"消息"];
//配置的各个tabbar对应的controller
self.vc3= [[UIViewControlleralloc]init];
self.vc3.view.backgroundColor= [UIColorcyanColor];
self.vc3.tabBarItem= [selfitemWithSelectedImage:@"tabBar_new_icon@2x"image:@"tabBar_new_icon@2x"title:@"发现"];
//配置的各个tabbar对应的controller
self.vc4= [[UIViewControlleralloc]init];
self.vc4.view.backgroundColor= [UIColororangeColor];
self.vc4.tabBarItem= [selfitemWithSelectedImage:@"tabBar_friendTrends_icon@3x"image:@"tabBar_friendTrends_icon@3x"title:@"我"];
//统一用tabbar来管理navigationController
self.viewControllers= @[self.vc1,self.vc2,self.vc3,self.vc4];
self.tabBar.tintColor= [UIColorgreenColor];
self.tabBar.barTintColor= [UIColorwhiteColor];
}
- (UITabBarItem*)itemWithSelectedImage:(NSString*)selectImageimage:(NSString*)imagetitle:(NSString*)title{
UIImage*im = [[UIImageimageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem*item = [[UITabBarItemalloc]initWithTitle:titleimage:imselectedImage:im];
returnitem;
}
然后自定义一个继承自UITabBar的类,在该类中设置UITabBar的子控件,在该类中定义一个UIButton属性,放在中间,系统自带的UIBarButton在两侧,将自定义的UIButton设置成懒加载代码如下:
//设置自己定义的button为懒加载并且设置上背景图片
- (UIButton*)btn{
if(_btn==nil) {
_btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
[_btnsetImage:[UIImageimageNamed:@"tabBar_publish_icon"]forState:UIControlStateNormal];
[_btnsetBackgroundImage:[UIImageimageNamed:@"tabBar_publish_icon"]forState:UIControlStateNormal];
//button的大小与图片一致
[_btnsizeToFit];
[selfaddSubview:self.btn];
}
return_btn;
}
来一个重新布局的方法//设置tabbar子控件的布局
- (void)layoutSubviews{
CGFloatw =self.bounds.size.width;
CGFloath =self.bounds.size.height;
CGFloatbtnx =0;
CGFloatbtny =0;
//5.0是tabbar中的控件的数量
CGFloatwidth =self.bounds.size.width/5.0;
CGFloatheight =self.bounds.size.height;
inti=0;
for(UIView*btninself.subviews) {
//判断是否是系统自带的UITabBarButton类型的控件
if([btnisKindOfClass:NSClassFromString(@"UITabBarButton")]) {
if(i==2) {
i=3;
}
btnx = i*width;
btn.frame=CGRectMake(btnx, btny, width, height);
i++;
}
}
//设置自定义button的位置
self.btn.center=CGPointMake(w*0.5, h*0.5);
}
然后再自定义的UITabbarController的viewDidLoad中定义一个自定义的UITabBar,并且替换掉系统自带的UITabBar,代码如下
[superviewDidLoad];
// Do any additional setup after loading the view.
WWTTabBar*tabBar = [[WWTTabBaralloc]initWithFrame:self.tabBar.frame];
tabBar.backgroundColor= [UIColorwhiteColor];
//设置tabbar时只能用keyValue方式
[selfsetValue:tabBarforKeyPath:@"tabBar"];