// CustomTabBarViewController.h #import <UIKit/UIKit.h> @interface CustomTabBarViewController : UITabBarController + (void)deallocTabbar; + (id)defaultsTabBar; - (void)selectBUttonWithIndex:(NSInteger)index; -(void)setTabBarHidden:(BOOL)Bool; @end
// // CustomTabBarViewController.m #import "CustomTabBarViewController.h" #import "DJSHomeViewController.h" #import "DJSStudioViewController.h" #import "DJSAppointmentViewController.h" #import "DJSMineViewController.h" #import "Header.h" #define btnBaseTag 100 static CustomTabBarViewController *defaults; @interface CustomTabBarViewController () { BOOL tabBarIsShow; } @property (nonatomic, strong) UIImageView *baceGroundImage; @property (nonatomic, strong) NSMutableArray *btnArray; @end @implementation CustomTabBarViewController @synthesize baceGroundImage, btnArray; + (void)deallocTabbar { defaults = nil; } + (id)defaultsTabBar { if (defaults == nil) { defaults = [[CustomTabBarViewController alloc] init]; } return defaults; } - (void)viewDidLoad { [super viewDidLoad]; tabBarIsShow = YES; self.view.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1]; self.tabBar.hidden = YES; DJSHomeViewController *home = [[DJSHomeViewController alloc]init]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:home]; DJSStudioViewController *produce = [[DJSStudioViewController alloc]init]; UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:produce]; DJSAppointmentViewController *appoint = [[DJSAppointmentViewController alloc]init]; UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:appoint]; DJSMineViewController *mine = [[DJSMineViewController alloc]init]; UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:mine]; self.viewControllers = @[nav, nav1, nav2, nav3]; btnArray = [[NSMutableArray alloc] init]; [self setCustomTabbarItem]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)setCustomTabbarItem { [btnArray removeAllObjects]; baceGroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, Main_height-54, Main_Width, 54)]; baceGroundImage.backgroundColor = [UIColor whiteColor]; baceGroundImage.image = [UIImage imageNamed:@"wireframe_bottom@2x"]; baceGroundImage.userInteractionEnabled = YES; [self.view addSubview:baceGroundImage]; // UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Width, 0.5)]; // lineView.backgroundColor = [UIColor cyanColor]; // [baceGroundImage addSubview:lineView]; NSArray *imageNameArr = @[@"home_unicorn_unpress", @"home_project_unpress", @"home_studio_unpress",@"home_my_unpress"]; NSArray *selecNameArr = @[@"home_unicorn_press", @"home_project_press", @"home_studio_press",@"home_my_press"]; CGFloat space = Main_Width/4; for (NSInteger i = 0; i < 4; i++) { UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i * space, 0, space, 54)]; // btn.backgroundColor = [UIColor cyanColor]; [btn setImage:[UIImage imageNamed:imageNameArr[i]] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:selecNameArr[i]] forState:UIControlStateSelected]; btn.tag = btnBaseTag + i; if (i == 0) { btn.selected = YES; } [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; [baceGroundImage addSubview:btn]; [btnArray addObject:btn]; } } - (void)selectBUttonWithIndex:(NSInteger)index { UIButton *btn = (UIButton *)btnArray[index]; [self pressBtn:btn]; } -(void)setTabBarHidden:(BOOL)Bool { if (Bool) { [self hideTabBar]; } else{ [self showTabBar]; } } - (void)hideTabBar { if (!tabBarIsShow) { //already hidden return; } [UIView animateWithDuration:0.35 animations:^{ baceGroundImage.frame = CGRectMake(0, Main_height, Main_Width, 54); }]; tabBarIsShow = NO; } - (void)showTabBar { if (tabBarIsShow) { // already showing return; } [UIView animateWithDuration:0.35 animations:^{ baceGroundImage.frame = CGRectMake(0, Main_height-54, Main_Width, 54); }]; tabBarIsShow = YES; } -(void)pressBtn:(id)sender { UIButton *btn=(UIButton *)sender; if (btn.tag - btnBaseTag >= self.viewControllers.count) { return; } self.selectedIndex = btn.tag - btnBaseTag; for (UIButton *tempBtn in btnArray) { if (tempBtn.tag == btn.tag) { tempBtn.selected=YES; } else { tempBtn.selected=NO; } } } @end
1、创建
@interface AppDelegate ()
{
CustomTabBarViewController *tabbar;
}
tabbar=[CustomTabBarViewController defaultsTabBar];
self.window.rootViewController = tabbar;
2、隐藏
CustomTabBarViewController * tabBar =[CustomTabBarViewController defaultsTabBar];
[tabBar setTabBarHidden:YES];