改变tabBar背景颜色的方法

2019-11-21  本文已影响0人  大宝来巡山
  // 方法1.
   [[UITabBar appearance] setBarTintColor:[UIColor greenColor]];
   [UITabBar appearance].translucent = NO;

 // 方法2.
//    UIView *view = [[UIView alloc]init];
//    view.backgroundColor = [UIColor redColor];
//    view.frame = self.tabBar.bounds;
//    [[UITabBar appearance] insertSubview:view atIndex:0];

 // 方法3.
//    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBarBackgroundImage"]];
//    [UITabBar appearance].translucent = NO;

/***   选中item背景色   ***/
   CGSize indicatorImageSize = CGSizeMake(self.tabBar.bounds.size.width / self.tabBar.items.count, self.tabBar.bounds.size.height + 34);

   self.tabBar.selectionIndicatorImage = [self drawTabBarItemBackgroundImageWithSize:indicatorImageSize];
   

- (UIImage *)drawTabBarItemBackgroundImageWithSize:(CGSize)size
{                                           
   // 准备绘图环境
   UIGraphicsBeginImageContext(size);
   CGContextRef ctx = UIGraphicsGetCurrentContext();
   
   CGContextSetRGBFillColor(ctx, 124.0 / 255, 124.0 / 255, 151.0 / 255, 1);
   CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));
   
   // 获取该绘图中的图片
   UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
   
   //结束绘图
   UIGraphicsEndImageContext();
   
   /*
   // 获取当前应用路径中Documents目录下指定文件名对应的文件路径
   NSString *path = [[NSHomeDirectory() stringByAppendingString:@"/Documents"] stringByAppendingString:@"/tab�BarBackgroundImage.png"];
   NSLog(@"path:%@", path);
   // 保存PNG图片
   [UIImagePNGRepresentation(img) writeToFile:path atomically:YES];
   */
   return img;
}

上一篇 下一篇

猜你喜欢

热点阅读