修改或者去掉Tabbar顶部的线

在做项目的时候,一般tabbar的背景颜色都需要我们自定义,背景颜色自定义好了,发现tabbar顶部的线还是有的,而且有的时候线的颜色和我们的需求不一致,特别是当tabbar背景颜色为白色等比较轻的颜色的时候。为了解决这个问题,下面提供了2个方法,分为用来去掉和修改tabbar顶部的线条颜色。

1、去掉tabbar顶部的线

//去掉顶部的线的效果,即阴影图片和背景图片一样时

CGRect rect = CGRectMake(0, 0, kMainWidth, kMainHeight);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);

CGContextFillRect(context, rect);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[self.tabBar setBackgroundImage:img];

[self.tabBar setShadowImage:img];


2、修改tabbar顶部线条的颜色

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

//可以设置线条的高度和颜色值,[QuickCreatTreasureBox hexChangeFloat:@"eeeeee"]这个是我封装的一个根据16进制数组来转成UIColor的方法,SCREEN_WIDTH是屏幕的宽度。

[self.tabBar setShadowImage:[self imageWithColor:[QuickCreatTreasureBox hexChangeFloat:@"eeeeee"] size:CGSizeMake(SCREEN_WIDTH, 0.5)]];

//根据颜色生成图片的函数

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {

if (!color || size.width <= 0 || size.height <= 0) return nil;

CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, color.CGColor);

CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容