自定義標(biāo)簽控制器

#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個導(dǎo)航控制器 3個視圖控制器

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:方法的時候 我們才有的選中圖片? ? ? ? ? ? ? ? 但是我們無法進入self.tabbar的文件內(nèi)部去復(fù)寫這個方法? ? ? ? ? ? 所以在控制器完全加載完成 即將顯示的時候創(chuàng)建,因為這時候,控制器個數(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個子視圖控制器? ? ? ? for (UIView *subView in self.tabBar.subviews) {? ? ? ? //2.標(biāo)簽欄上創(chuàng)建的是 UITabBarButton ,這個類是內(nèi)部類,我們無法使用,所以移除原有按鈕? ? ? ? [subView removeFromSuperview];? ? }? ? ? ? //3.添加自定義按鈕? ? for (int i = 0; i標(biāo)簽項->圖片+標(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à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.切換控制器的 選中圖片動畫

[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;//保存默認圖片

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) {

//保存默認圖片

_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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容