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;
/**
* 分析空間功能和信息來(lái)源
1.當(dāng)設(shè)置viewControllers的時(shí)候 -> 按鈕個(gè)數(shù) 按鈕寬度
2.item的image和title 來(lái)源是 -> VC控制器
3.選中圖片的位置 -> 點(diǎn)擊item調(diào)用的代理方法 index
4.背景圖 -> 子類(lèi)化得標(biāo)簽控制器的屬性
*/
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
/**
* 通過(guò)frame和tabBarItem創(chuàng)建按鈕的方法
*/
- (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];
/**
*
執(zhí)行完上面的方法
可以得到當(dāng)前本控制器管理的視圖控制器個(gè)數(shù)
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) -> 將字符串轉(zhuǎn)化為對(duì)應(yīng)的類(lèi)名
UITabBarButton -> 標(biāo)簽控制器定義的內(nèi)部類(lèi)[tabbar上的按鈕]
*/
if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[subView removeFromSuperview];
}
}
}
/**
* 2.創(chuàng)建新的有背景圖的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.創(chuàng)建標(biāo)簽按鈕
*/
- (void)createTabBarButton{
//1.獲取控制器個(gè)數(shù)
NSInteger count = self.viewControllers.count;
//2.選中的背景圖片
_selectIV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, KScreenWidth/count -20, 44)];
_selectIV.image = _selectImg;
[_customTabBar addSubview:_selectIV];
/*——————————————————————————————————————————————————————————————————————————————-*/
//創(chuàng)建按鈕
//1.根據(jù)個(gè)數(shù)創(chuàng)建按鈕
for (int i = 0; i <count; i++) {
//計(jì)算按鈕的frame
CGRect itemFrame = CGRectMake(i *KScreenWidth/count, 0, KScreenWidth/count, 49);
//獲取按鈕對(duì)應(yīng)的控制器的item
UITabBarItem *tabBarItem = self.viewControllers[i].tabBarItem;
//創(chuàng)建
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.獲取點(diǎn)擊按鈕的index
NSInteger index = item.tag - 100;
//2.更改當(dāng)前顯示的VC
self.selectedIndex = index;
/*——————————————————————————————————————————————————————————————————————————————-*/
//選中圖片移動(dòng)動(dòng)畫(huà)
[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:包含創(chuàng)建item所需要的title和image
*/
//創(chuàng)建ImageView Label
//1.Label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 20)];
//居中
label.textAlignment = NSTextAlignmentCenter;
//字號(hào)
label.font = [UIFont systemFontOfSize:10];
//顏色
label.textColor = [UIColor whiteColor];
//內(nèi)容 --> 從tabBarItem獲取
label.text = tabBarItem.title;
[self addSubview:label];
//2.Image
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-29)/2, 20, 29, 29)];
//內(nèi)容模式
imageView.contentMode = UIViewContentModeScaleAspectFit;
//圖片 --> 從tabbarItem獲取
imageView.image = tabBarItem.image;
[self addSubview:imageView];
}
return self;
}
@end
屏幕快照 2016-03-08 下午8.47.47.png