AppDelegate.m
#import "AppDelegate.h"
#import "CustomTabBarController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
/*——————————————————————————————————————————————————————————————————————————————-*/
UIViewController *vc0 = [[UIViewController alloc]init];
UIViewController *vc1 = [[UIViewController alloc]init];
UIViewController *vc2 = [[UIViewController alloc]init];
UIViewController *vc3 = [[UIViewController alloc]init];
UIViewController *vc4 = [[UIViewController alloc]init];
vc0.title = @"HOME";
vc0.tabBarItem.image = [UIImage imageNamed:@"home.png"];
vc1.title = @"INFO";
vc1.tabBarItem.image = [UIImage imageNamed:@"myinfo.png"];
vc2.title = @"payticket";
vc2.tabBarItem.image = [UIImage imageNamed:@"payticket"];
vc3.title = @"discover";
vc3.tabBarItem.image = [UIImage imageNamed:@"discover"];
vc4.title = @"store";
vc4.tabBarItem.image = [UIImage imageNamed:@"store"];
UINavigationController *nav0 = [[UINavigationController alloc]initWithRootViewController:vc0];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:vc4];
//1.
CustomTabBarController *tbc = [[CustomTabBarController alloc]initWithSelectedImge:[UIImage imageNamed:@"选中"] tabBarBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
//2.
tbc.viewControllers = @[nav0,nav1,nav2,nav3,nav4];
tbc.selectedIndex = 2;
/**
* 分析空间功能和信息来源
1.当设置viewControllers的时候 -> 按钮个数 按钮宽度
2.item的image和title 来源是 -> VC控制器
3.选中图片的位置 -> 点击item调用的代理方法 index
4.背景图 -> 子类化得标签控制器的属性
*/
self.window.rootViewController = tbc;
return YES;
}
@end
CustomTabBarController.h
#import <UIKit/UIKit.h>
@class CustomTabBarItem;
@interface CustomTabBarController : UITabBarController
- (instancetype)initWithSelectedImge:(UIImage *)selectedImage tabBarBackgroundImage:(UIImage *)bgImage;
@end
@interface CustomTabBarItem : UIButton
/**
* 通过frame和tabBarItem创建按钮的方法
*/
- (instancetype)initWithFrame:(CGRect)frame tabBarItem:(UITabBarItem *)tabBarItem;
@end
CustomTabBarController.m
#import "CustomTabBarController.h"
#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UISreen mainScreen].bounds.size.height
@interface CustomTabBarController ()
@property (nonatomic,strong) UIView *customTabBar;
@property (nonatomic,strong) UIImage *selectImg;
@property (nonatomic,strong) UIImage *bgImg;//私有属性保存背景图片
@property (nonatomic,strong) UIImageView *selectIV;//选中item背景图片视图
@end
@implementation CustomTabBarController
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
_selectIV.frame = CGRectMake(10 +self.selectedIndex *KScreenWidth/self.viewControllers.count, 5, KScreenWidth/self.viewControllers.count -20, 44);
}
- (instancetype)initWithSelectedImge:(UIImage *)selectedImage tabBarBackgroundImage:(UIImage *)bgImage{
if (self = [super init]) {
_selectImg = selectedImage;
_bgImg = bgImage;
}
return self;
}
//重
- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers{
[super setViewControllers:viewControllers];
/**
*
执行完上面的方法
可以得到当前本控制器管理的视图控制器个数
1.移除原有的按钮 tabbarItem
2.添加自定义的 tabbarItem
3.更改背景图片
4.自定义的选中图片
*/
[self removeTabBarButtons];
[self createNewTaBar];
[self createTabBarButton];
}
#pragma mark --1.移除原有的所有tabBarButton
/**
* 1.移除tabbar上所有的tabbarItem
*/
- (void)removeTabBarButtons{
NSLog(@"%@",self.tabBar.subviews);
for (UIView *subView in self.tabBar.subviews) {
/**
* NSClassFromString(NSString *aClassName) -> 将字符串转化为对应的类名
UITabBarButton -> 标签控制器定义的内部类[tabbar上的按钮]
*/
if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[subView removeFromSuperview];
}
}
}
/**
* 2.创建新的有背景图的tabbar
*/
- (void)createNewTaBar{
_customTabBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 49)];
_customTabBar.backgroundColor = [UIColor colorWithPatternImage:[_bgImg stretchableImageWithLeftCapWidth:160 topCapHeight:25]];
[self.tabBar addSubview:_customTabBar];
}
/**
* 3.创建标签按钮
*/
- (void)createTabBarButton{
//1.获取控制器个数
NSInteger count = self.viewControllers.count;
//2.选中的背景图片
_selectIV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, KScreenWidth/count -20, 44)];
_selectIV.image = _selectImg;
[_customTabBar addSubview:_selectIV];
/*——————————————————————————————————————————————————————————————————————————————-*/
//创建按钮
//1.根据个数创建按钮
for (int i = 0; i <count; i++) {
//计算按钮的frame
CGRect itemFrame = CGRectMake(i *KScreenWidth/count, 0, KScreenWidth/count, 49);
//获取按钮对应的控制器的item
UITabBarItem *tabBarItem = self.viewControllers[i].tabBarItem;
//创建
CustomTabBarItem *item = [[CustomTabBarItem alloc]initWithFrame:itemFrame tabBarItem:tabBarItem];
//tag值
item.tag = 100 +i;
//添加事件
[item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
[_customTabBar addSubview:item];
}
}
- (void)itemClick:(UIButton *)item{
//切换视图控制器
//1.获取点击按钮的index
NSInteger index = item.tag - 100;
//2.更改当前显示的VC
self.selectedIndex = index;
/*——————————————————————————————————————————————————————————————————————————————-*/
//选中图片移动动画
[UIView animateWithDuration:0.25 animations:^{
_selectIV.frame = CGRectMake(10 +self.selectedIndex *KScreenWidth/self.viewControllers.count, 5, KScreenWidth/self.viewControllers.count -20, 44);
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
@implementation CustomTabBarItem
- (instancetype)initWithFrame:(CGRect)frame tabBarItem:(UITabBarItem *)tabBarItem{
if (self = [super initWithFrame:frame]) {
/**
* tabBarItem:包含创建item所需要的title和image
*/
//创建ImageView Label
//1.Label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 20)];
//居中
label.textAlignment = NSTextAlignmentCenter;
//字号
label.font = [UIFont systemFontOfSize:10];
//颜色
label.textColor = [UIColor whiteColor];
//内容 --> 从tabBarItem获取
label.text = tabBarItem.title;
[self addSubview:label];
//2.Image
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-29)/2, 20, 29, 29)];
//内容模式
imageView.contentMode = UIViewContentModeScaleAspectFit;
//图片 --> 从tabbarItem获取
imageView.image = tabBarItem.image;
[self addSubview:imageView];
}
return self;
}
@end