// // BPViewController.m // 家有宝贝 // // Created by wuli萍萍 on 16/5/21. // Copyright © 2016年 wuli萍萍. All rights reserved. // #import "CustomTabBarController.h" #import "HomePageVC.h" #import "MallHomeVC.h" #import "MineHomeVC.h" #import "CustomTabBar.h" #import "xingchuangke-Swift.h" @interface CustomTabBarController () @end @implementation CustomTabBarController #pragma mark 懒加载 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //去掉分割线 GB_Nav.navigationBar.shadowImage = [UIImage new]; } - (void)viewDidLoad { [super viewDidLoad]; self.delegate = self; [self.view setBackgroundColor:COLOR_BACKGROUND]; // 添加子控制器 [self setUpChildVC:[HomePageVC new] title:@"首页" image:@"home_un" selectedImage:@"home"]; [self setUpChildVC:[ShoppingMallCollectionViewController new] title:@"商城" image:@"aide_un" selectedImage:@"aide"]; [self setUpChildVC:[MyViewController new] title:@"我的" image:@"mine_un" selectedImage:@"mine"]; // 设置tabbar的背景图片 [[CustomTabBar appearance]setBackgroundColor:[UIColor whiteColor]]; [[CustomTabBar appearance]setShadowImage:[[UIImage alloc]init]];//将TabBar上的黑线去掉 CustomTabBar *tabBar = [[CustomTabBar alloc] init]; // 设置代理 tabBar.delegate = self; [self setValue:tabBar forKey:@"tabBar"]; // [self setupTabBar]; } - (void)setupTabBar{ UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds]; bgView.backgroundColor = [UIColor whiteColor]; //[self.tabBar insertSubview:bgView atIndex:0]; self.tabBar.opaque = YES; } #pragma mark ---- 初始化子控制器 - (void)setUpChildVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage { vc.title = title; vc.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 禁用图片渲染 vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //设置字体和图片的距离 CGFloat spaceTop; CGFloat imageTop; CGFloat fontSize; if (SCREEN_WIDTH == 320) { imageTop = -2; fontSize = 9; spaceTop = -4; }else if (SCREEN_WIDTH == 375){ imageTop = -2; fontSize = 11; spaceTop = -1; }else if (SCREEN_WIDTH == 414){ imageTop = -2; fontSize = 12; spaceTop = -5; }else{ imageTop = -2; spaceTop = -5; fontSize = 11; } [vc.tabBarItem setImageInsets:UIEdgeInsetsMake(imageTop,0, -imageTop, 0)]; [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, spaceTop)]; // if (@available(iOS 13.0, *)) { // UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy]; // // 设置未被选中的颜色 // appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName :COLOR_TEXT_UNSELECT,NSFontAttributeName:[UIFont systemFontOfSize:F(12)]}; // // 设置被选中时的颜色 // appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName :[UIColor blackColor] ,NSFontAttributeName:[UIFont systemFontOfSize:F(12)]}; // appearance.shadowColor = [UIColor clearColor]; // [appearance configureWithTransparentBackground]; //// if ((SCREEN_HEIGHT == 812||SCREEN_HEIGHT == 896)) { //// appearance.backgroundImage=[UIImage imageNamed:@"background_tab_375"]; //// }else{ //// appearance.backgroundImage=[UIImage imageNamed:@"background_tab"]; //// } // vc.tabBarController.tabBar.standardAppearance = appearance; // // }else{ // 设置文字的样式 [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :COLOR_TEXT_UNSELECT,NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} forState:UIControlStateNormal]; [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor blackColor] ,NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} forState:UIControlStateSelected]; self.tabBar.backgroundColor = [UIColor clearColor]; // if ((SCREEN_HEIGHT == 812||SCREEN_HEIGHT == 896)) { // self.tabBar.backgroundImage=[UIImage imageNamed:@"background_tab_375"]; // }else if (SCREEN_WIDTH==414){ // self.tabBar.backgroundImage=[UIImage imageNamed:@"background_tab_414"]; // } else{ // self.tabBar.backgroundImage=[UIImage imageNamed:@"background_tab"]; // } [self.tabBar setShadowImage:[UIImage new]];//将TabBar上的黑线去掉 // } // if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) { // [UINavigationBar appearance].tintColor = [UIColor whiteColor]; // [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName : COLOR_LABEL}]; // [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]]; // [[UINavigationBar appearance] setTranslucent:NO]; // } // 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器 [self addChildViewController:vc]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; CGFloat tabberHeight = TABBAR_HEIGHT; // if (SCREEN_WIDTH == 320){ // tabberHeight = 50; // }else if(SCREEN_WIDTH == 375){ // tabberHeight = IS_iPhoneX ? 83 : 53; // }else{ // tabberHeight = 55; // } CGRect tabFrame = self.tabBar.frame; //self.TabBar is IBOutlet of your TabBar tabFrame.size.height = tabberHeight; tabFrame.origin.y = self.view.frame.size.height - tabberHeight; self.tabBar.frame = tabFrame; } #pragma mark tabbarVC delegate -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ return true; } @end