美文网首页
iOS开发中的小技巧15:修改或者去掉Tabbar上边的线

iOS开发中的小技巧15:修改或者去掉Tabbar上边的线

作者: 莫离_焱 | 来源:发表于2017-08-26 15:12 被阅读38次

    tabbar的顶部有一条线,有时这条线会影响整体的视觉效果,所以,此时就需要将线去掉或者更改一下线的颜色。

    这个操作一般放在根视图(UITabBarController)中处理:

    1.去掉线

    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    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.更改线的颜色

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

    - (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;

    }

    更改方法,缺一不可

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

    [self.tabBar setShadowImage:[self imageWithColor:[UIColor redColor] size:CGSizeMake(SCREEN_W, 0.3)]];

    3.更改tabbar背景颜色

    [self.tabBar setBarTintColor:[UIColor whiteColor]];

    self.tabBar.translucent = NO;//必须加上这一句

    区别与self.tabBar.tintColor = [UIColor blueColor];方法,这个方法是用来更改选中的tabbar文字的颜色,不是背景色,两个看着相似,但是则不同。

    相关文章

      网友评论

          本文标题:iOS开发中的小技巧15:修改或者去掉Tabbar上边的线

          本文链接:https://www.haomeiwen.com/subject/yyomdxtx.html