//
// 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"
@interface ZZTabBarController () <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