iOS去除UITabBar黑线

一、在iOS13之前,我们去除黑线是这样设置的

[self.tabBar setBackgroundImage:[UIImage new]];
[self.tabBar setShadowImage:[UIImage new]];

二、iOS13引入了UITabBarAppearance类,所以我们去除黑线需要这样设置

if(@available(iOS 13.0, *)) {
   UITabBarAppearance *appearance = [UITabBarAppearance new];
   appearance.backgroundColor = [UIColor clearColor];
   appearance.backgroundImage = [UIImage new];
   appearance.shadowColor = [UIColor clearColor];
   appearance.shadowImage = [UIImage new];
   [UITabBar appearance].standardAppearance = appearance;
    }

三、给tabBar设置文字样式,页面跳转返回后,在iOS13会无效,变成系统蓝了

- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = kHexColor(0x939393);
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = kHexColor(0x1A1A1A);
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];

    [self addChildViewController:childVc];
}

解决:也是iOS13新加的UITabBarAppearance类有关

- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = kHexColor(0x939393);
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = kHexColor(0x1A1A1A);
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 解决出现系统蓝问题    
    if(@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [UITabBarAppearance new];
        // 设置未被选中的颜色
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: kHexColor(0x939393)};
        // 设置被选中时的颜色
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: kHexColor(0x1A1A1A)};
        [UITabBar appearance].standardAppearance = appearance;
    }
    [self addChildViewController:childVc];
}

你可能感兴趣的:(iOS去除UITabBar黑线)