viewDidLoad中设置
//设置tabBar的颜色
UIView *bgView = [[UIViewalloc] initWithFrame:self.tabBar.bounds];
bgView.backgroundColor = [UIColorblueColor];
[self.tabBarinsertSubview:bgView atIndex:0];
self.tabBar.opaque =YES;
背景颜色:
[[UITabBar appearance]setBackgroundImage:[[UIImage alloc]init]];
[[UITabBar appearance]setBackgroundColor:[UIColor whiteColor]];
shadow设置:
[[UITabBar appearance]setShadowImage:[[UIImage imageNamed:@"bottomshadown"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
tabbaritem设置:
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
如果在一些系统上部自作用,可以试试下面的
// iOS 13以上
self.tabBar.tintColor = [UIColor redColor];
self.tabBar.unselectedItemTintColor = [UIColor grayColor];
- (void)viewDidLoad {
[super viewDidLoad];
[self setTabBar];
[self addChildVC];
}
-(void)setTabBar{
//1.插入ttabBar的背景
// UIView *customBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(self.tabBar.bounds.origin.x, self.tabBar.bounds.origin.y, self.tabBar.bounds.size.width, self.tabBar.bounds.size.height+BottomHeight)];
// customBackgroundView.backgroundColor = [UIColor whiteColor];
// [[UITabBar appearance] insertSubview:customBackgroundView atIndex:0];
//2.直接设置背景
[[UITabBar appearance]setBackgroundImage:[[UIImage alloc]init]];
[[UITabBar appearance]setBackgroundColor:[UIColor whiteColor]];
//shadow设置图片
[[UITabBar appearance]setShadowImage:[[UIImage imageNamed:@"bottomshadown"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
// [self removetopblackline];//去掉顶部黑线
}
//去除tabBar顶部黑线
-(void)removetopblackline{
if(@available(iOS 13.0, *)) {
UITabBarAppearance* tabbarAppearance =[UITabBarAppearance new];
tabbarAppearance.shadowImage = [self imageWithColor:[UIColor clearColor]];
//或者下面
// tabbarAppearance.shadowColor=[UIColor clearColor];
self.tabBar.standardAppearance=tabbarAppearance;
}else{
// self.tabBar.backgroundImage = [UIImage new];
self.tabBar.shadowImage = [UIImage new];
}
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
-(void)addChildVC{
LYBHomeVC *homeVC = [LYBHomeVC new];
[self setupChildVc:homeVC title:@"" tabbartitle:@"首页" image:@"tab_home_icon" selectedImage:@"tab_home_pre_icon"];
LYBDiscoveryVC *discoveryVC = [LYBDiscoveryVC new];
[self setupChildVc:discoveryVC title:@"" tabbartitle:@"发现" image:@"tab_find_icon" selectedImage:@"tab_find_pre_icon"];
LYBYueqiVC *circleVC = [LYBYueqiVC new];
[self setupChildVc:circleVC title:@"约起" tabbartitle:@"约起" image:@"tab_yueqi_icon" selectedImage:@"tab_yueqi_pre_icon"];
LYBMyVC *myVC = [LYBMyVC new];
[self setupChildVc:myVC title:@"" tabbartitle:@"我的" image:@"tab_wode_icon" selectedImage:@"tab_wode_pre_icon"];
}
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title tabbartitle:(NSString *)tabbarTitle image:(NSString *)image selectedImage:(NSString *)selectedImage
{
vc.navigationItem.title = title;
QMWNBasenav *nav = [[QMWNBasenav alloc] initWithRootViewController:vc];
nav.tabBarItem.title=tabbarTitle;
// nav.tabBarItem.imageInsets = UIEdgeInsetsMake(10,0, -10,0);
[nav.tabBarItem setImage:[[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[nav.tabBarItem setSelectedImage:[[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
// if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [UITabBarAppearance new];
// 设置未被选中的颜色
appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#CED8E1FF"],NSFontAttributeName:[UIFont systemFontOfSize:12]};
// 设置被选中时的颜色
appearance.stackedLayoutAppearance.selected.titleTextAttributes =@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#282F56FF"],NSFontAttributeName:[UIFont systemFontOfSize:12]};
nav.tabBarItem.standardAppearance = appearance;
//
// self.tabBar.unselectedItemTintColor=[UIColor colorWithHexString:@"#CED8E1FF"];
// self.tabBar.barTintColor=[UIColor colorWithHexString:@"#282F56FF"];
// } else {
// [nav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#CED8E1FF"],NSFontAttributeName:[UIFont systemFontOfSize:12]}forState:UIControlStateNormal];//未选中
// [nav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#282F56FF"],NSFontAttributeName:[UIFont systemFontOfSize:12]}forState:UIControlStateSelected];//选中
// }
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#CED8E1FF"],NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateSelected];
[self addChildViewController:nav];
}