#import "AppDelegate.h"
#import "CRITabBarController.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];
/*————————————————————————————————————————————————————————————————————————*/
NSMutableArray *viewControllers = [NSMutableArray array];
NSArray *titles = @[@"主頁",@"信息",@"購票",@"發(fā)現(xiàn)",@"商店"];
NSArray *imgNames = @[@"home",@"myinfo",@"payticket",@"discover",@"store"];
for (int i = 0; i<5; i++) {
//假設(shè) 2個(gè)導(dǎo)航控制器 3個(gè)視圖控制器
UIViewController *viewController = nil;
UIViewController *vc = [[UIViewController alloc]init];
vc.title = titles[i];
vc.tabBarItem.image = [UIImage imageNamed:imgNames[i]];
vc.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_on",imgNames[i]]];
vc.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
viewController = vc;
if (i<2) {
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
viewController = nav;
}
[viewControllers addObject:viewController];
}
//標(biāo)簽
CRITabBarController *tabbar = [[CRITabBarController alloc]init];
tabbar.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"選中"];
tabbar.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bg_all"];
tabbar.viewControllers = viewControllers;
tabbar.selectedIndex = 3;
self.window.rootViewController = tabbar;
return YES;
}
#import//自定義的標(biāo)簽控制器類
@interface CRITabBarController : UITabBarController
@end
//自定義的垂直button類
@interface VerticalButton : UIButton
@end
#import "CRITabBarController.h"#define kTabBarWidth self.tabBar.frame.size.width#define kTabBarHeight self.tabBar.frame.size.height#define kButtonWidth kTabBarWidth/self.viewControllers.count@interface CRITabBarController (){? ? UIImageView *_selectImgV;}@property (nonatomic,strong) NSMutableArray *tabBarButtons;@end@implementation CRITabBarController- (instancetype)init{? ? self = [super init];? ? if (self) {? ? ? ? _tabBarButtons = [NSMutableArray array];? ? }? ? return self;}- (void)viewWillAppear:(BOOL)animated{? ? ? ? [super viewWillAppear:animated];? ? ? ? /**? ? * 8.當(dāng)控制器的tabbar執(zhí)行 setSelectionIndicatorImage:方法的時(shí)候 我們才有的選中圖片? ? ? ? ? ? ? ? 但是我們無法進(jìn)入self.tabbar的文件內(nèi)部去復(fù)寫這個(gè)方法? ? ? ? ? ? 所以在控制器完全加載完成 即將顯示的時(shí)候創(chuàng)建,因?yàn)檫@時(shí)候,控制器個(gè)數(shù)+按鈕寬度+選中的控制器? ? ? ? 都確定了? ? */? ? ? ? //檢查是否有選中圖片? ? UIImage *selectionIndicatorImage = self.tabBar.selectionIndicatorImage;? ? ? ? //如果沒有 return 什么都不做? ? if (selectionIndicatorImage == nil)return;? ? ? ? ? ? //如果有圖片:創(chuàng)建選中圖片-> 圖片+frame? ? _selectImgV = [[UIImageView alloc]initWithImage:selectionIndicatorImage];? ? _selectImgV.frame = CGRectMake(self.selectedIndex * kButtonWidth, 0, kButtonWidth, kTabBarHeight);? ? [self.tabBar insertSubview:_selectImgV atIndex:0];? ? ? ? //將選中的button高亮? ? UIButton *selectButton = self.tabBarButtons[self.selectedIndex];? ? selectButton.selected = YES;? ? ? ? }/** *? 外部給本標(biāo)簽控制器 子控制器數(shù)組賦值的方法 */- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers{? ? ? ? [super setViewControllers:viewControllers];? ? ? ? /*————————————————————————————————————————————————————————————————————————*/? ? //1.標(biāo)簽控制器已經(jīng)有了N個(gè)子視圖控制器? ? ? ? for (UIView *subView in self.tabBar.subviews) {? ? ? ? //2.標(biāo)簽欄上創(chuàng)建的是 UITabBarButton ,這個(gè)類是內(nèi)部類,我們無法使用,所以移除原有按鈕? ? ? ? [subView removeFromSuperview];? ? }? ? ? ? //3.添加自定義按鈕? ? for (int i = 0; i標(biāo)簽項(xiàng)->圖片+標(biāo)題 ->用于button的圖片+標(biāo)題
UIImage *image = subVC.tabBarItem.image;
UIImage *selectImage = subVC.tabBarItem.selectedImage;
NSString *title = subVC.tabBarItem.title;
//4.普通button的圖片和標(biāo)題是水平排布的,不符合我們的要求,--->自定義垂直排布的button
[button setImage:image forState:UIControlStateNormal];
[button setImage:selectImage forState:UIControlStateSelected];
[button setTitle:title forState:UIControlStateNormal];
[self.tabBar addSubview:button];
[self.tabBarButtons addObject:button];//?將按鈕交給數(shù)組管理
//7.添加事件,將按鈕添加 切換視圖控制器的功能
[button addTarget:self action:@selector(selectedVC:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 100+i;
}
}
/**
*? 點(diǎn)擊按鈕調(diào)用的方法
*/
- (void)selectedVC:(UIButton *)button{
//7.1 修改標(biāo)簽控制器的本身具有的 selectedIndex 完成切換功能
self.selectedIndex = button.tag -100;
//10.將除了選中的button之外的按鈕選中狀態(tài)改為no
for (UIButton *btn in self.tabBarButtons) {
btn.selected = NO;
}
button.selected = YES;
//9.切換控制器的 選中圖片動(dòng)畫
[UIView animateWithDuration:0.3 animations:^{
_selectImgV.frame = CGRectMake(self.selectedIndex * kButtonWidth, _selectImgV.frame.origin.y, _selectImgV.frame.size.width, _selectImgV.frame.size.height);
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
#pragma mark -- 垂直布局的Button
@interface VerticalButton ()
{
UILabel *_subLabel;//按鈕的標(biāo)題
UIImageView *_subImageView;//按鈕的圖片視圖
UIImage *_normalImg;//保存默認(rèn)圖片
UIImage *_selectImg;//保存選中圖片
}
@end
@implementation VerticalButton
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//5. 創(chuàng)建子視圖 Label 上 + ImageView 下
_subLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height * 0.3)];
_subLabel.textAlignment = NSTextAlignmentCenter;
_subLabel.textColor = [UIColor whiteColor];
_subLabel.font = [UIFont systemFontOfSize:12];
[self addSubview:_subLabel];
_subImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, _subLabel.frame.size.height, frame.size.width, frame.size.height *0.7)];
_subImageView.contentMode = UIViewContentModeCenter;
[self addSubview:_subImageView];
}
return self;
}
//6.截獲button的 圖片和標(biāo)題 的賦值方法,將圖片和標(biāo)題用于 Label + ImageView
- (void)setSelected:(BOOL)selected{
[super setSelected:selected];
if (self.selected == YES) {
_subImageView.image = _selectImg;
}else{
_subImageView.image = _normalImg;
}
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state{
if (state == UIControlStateNormal) {
//保存默認(rèn)圖片
_normalImg = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[_subImageView setImage:_normalImg];
}else if (state == UIControlStateSelected){
//保存選中圖片
_selectImg = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state{
if (state == UIControlStateNormal) {
//保存標(biāo)題
[_subLabel setText:title];
}
}
@end