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

在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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容