.h
-(void)addTabBarButtonWithItem:(UITabBarItem *)itme
.m
#import "LJFTabbar.h"
@implementation LJFTabbar
-(void)addTabBarButtonWithItem:(UITabBarItem *)itme{
//1.创建按钮
UIButton *button = [[UIButtonalloc]init];
[selfaddSubview:button];
[button setTitle:itme.titleforState:UIControlStateNormal];
[button setImage:itme.imageforState:UIControlStateNormal];
[button setImage:itme.selectedImageforState:UIControlStateSelected];
}
-(void)layoutSubviews{
[superlayoutSubviews];
CGFloat buttonW =self.frame.size.width/self.subviews.count ;
CGFloat buttonH =self.frame.size.height;
CGFloat buttonY =0 ;
for (int index =0; index <self.subviews.count; index++) {
//1.取出按钮
UIButton *button =self.subviews[index];
//2. 设置按钮的frame
CGFloat buttonX = index * buttonW;
button.frame =CGRectMake(buttonX, buttonY, buttonW, buttonH) ;
}
}
自定义tabber
-(void)setupTabbar{
LJFTabbar *costomTabbar = [[LJFTabbaralloc]init];
costomTabbar.backgroundColor = [UIColorredColor];
costomTabbar.frame =self.tabBar.bounds;
//[costomTabbar addTabBarButtonWithTitle:@"" imagename:@"" selectedImageName:@""];
[self.tabBaraddSubview:costomTabbar];
self.costomTabbar = costomTabbar;
}
/**
* 初始化子控制器childVc
* title 标题
* 图标
* 选中的图标
*
***/
-(void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imagename:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
//设置控制器属性
childVc.title = title;
//把对系统的图片返回进行封装
childVc.tabBarItem.image = [UIImageimageWithName:imageName];
// childVc.tabBarItem.image = [UIImage imageNamed:imageName];
// childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childVc.tabBarItem.selectedImage = [[UIImageimageWithName:selectedImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//包装导航控制器
UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:childVc];
[selfaddChildViewController:nav];
关键:
[self.costomTabbaraddTabBarButtonWithItem:childVc.tabBarItem];
}