程序开发

iOS开发丨iOS 13隐藏tabbar黑色线条并设置文本颜色

2019-12-20  本文已影响0人  炼心术师

在iOS13中底部tabbar会出现一个烦人的黑色线条,并且使用之前的方法无法去除掉,因为tabbar增加了新的属性standardAppearance。

// 隐藏tabbar黑色线条
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *tabbarAppearance = self.tabBar.standardAppearance;
        tabbarAppearance.shadowImage = [APPThemeStandard imageWithColor:[UIColor clearColor] size:CGSizeMake(self.tabBar.frame.size.width, 0.5)];
        tabbarAppearance.backgroundImage = [APPThemeStandard imageWithColor:[UIColor whiteColor] size:self.tabBar.bounds.size];
        tabbarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor]};
        tabbarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};
        self.tabBar.standardAppearance = tabbarAppearance;
    }

设置一个高度为0.5、颜色为Clear的shadowImage就可以隐藏线条了,在iOS13中还用normal.titleTextAttributes和selected.titleTextAttributes替换了原来的setTitleTextAttributes方法,因此用老的方法设置tabbar文本选中颜色是不起作用的。

生成纯色图片:

/// 通过颜色来生成一个纯色图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, 3);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
上一篇 下一篇

猜你喜欢

热点阅读