iOS開發-自定義TabBarController新增跳轉控制器並且進行封裝(根據自定義TabBar)
//
// ZZTabBarController.h
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/10/9.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZZTabBarController :UITabBarController
@end
//
// ZZTabBarController.m
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/10/9.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import "ZZTabBarController.h"
#import "ZZHomeController.h"
#import "ZZMeController.h"
#import "ZZDiscoverController.h"
#import "ZZMessageController.h"
#import "ZZComposeController.h"
#import "ZZTabBar.h"
#import "ZZNavigationController.h"
@interfaceZZTabBarController () <ZZTabBarDelegate>
@property (nonatomic,weak)ZZTabBar *customTabBar;
@end
@implementation ZZTabBarController
- (void)viewDidLoad {
[superviewDidLoad];
// 1.初始化tabbar
[selfsetupTabBar];
// 2.初始化子控制器
[selfsetupChildViewControllers];
}
/**
* 每次即將顯示的時候必須移除系統自帶的
*/
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
// 移除之前的4個UITabBarButton
for (UIView *childinself.tabBar.subviews) {
if ([childisKindOfClass:[UIControlclass]]) {//UITabBarButton
[childremoveFromSuperview];
}
}
}
/**
* 初始化tabbar
*/
- (void)setupTabBar
{
ZZTabBar *customTabBar = [[ZZTabBaralloc]init];
// 自定義的tabbar 覆蓋系統的就行
customTabBar.frame =self.tabBar.bounds;
customTabBar.delegate =self;
[self.tabBaraddSubview:customTabBar];
self.customTabBar = customTabBar;
}
/**
* 初始化子控制器
*/
- (void)setupChildViewControllers
{
// 1.首頁
ZZHomeController *home = [[ZZHomeControlleralloc]init];
home.tabBarItem.badgeValue =@"4";
[selfsetupChildViewController:hometitle:@"首頁"imageName:@"tabbar_home"selectedImageName:@"tabbar_home_selected"];
// 2.訊息
ZZMessageController *message = [[ZZMessageControlleralloc]init];
message.tabBarItem.badgeValue =@"40";
[selfsetupChildViewController:messagetitle:@"訊息"imageName:@"tabbar_message_center"selectedImageName:@"tabbar_message_center_selected"];
// 3.廣場
ZZDiscoverController *discover = [[ZZDiscoverControlleralloc]init];
discover.tabBarItem.badgeValue =@"10";
[selfsetupChildViewController:discovertitle:@"廣場"imageName:@"tabbar_discover"selectedImageName:@"tabbar_discover_selected"];
// 4.我
ZZMeController *me = [[ZZMeControlleralloc]init];
me.tabBarItem.badgeValue =@"5";
[selfsetupChildViewController:metitle:@"我"imageName:@"tabbar_profile"selectedImageName:@"tabbar_profile_selected"];
}
/**
* 新增一個子控制器
*
* @param vc 子控制器
* @param title 標題
* @param imageName 圖片
* @param selectedImageName 選中的圖片
*/
- (void)setupChildViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
// 1.設定資料
vc.title = title;
vc.tabBarItem.image = [UIImageimageWithName:imageName];
vc.tabBarItem.selectedImage = [UIImageoringinalImageWithName:selectedImageName];
// 2.包裝
ZZNavigationController *nav = [[ZZNavigationControlleralloc]initWithRootViewController:vc];
[selfaddChildViewController:nav];
// 3.新增按鈕
[self.customTabBaraddTabBarButtonWithItem:vc.tabBarItem];
}
/**
* tabBar選中了按鈕
*
* @param from 以前的位置
* @param to 現在的位置
*/
- (void)tabBar:(ZZTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to
{
self.selectedIndex = to;
}
/**
* 選中+號按鈕
*/
- (void)tabBarDidClickedPlusButton:(ZZTabBar *)tabBar
{
ZZComposeController *compose = [[ZZComposeControlleralloc]init];
ZZNavigationController *nav = [[ZZNavigationControlleralloc]initWithRootViewController:compose];
[selfpresentViewController:navanimated:YEScompletion:nil];
}
@end