先附上效果图
在UITabBarController的viewDidLoad中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
#pragma mark - 实现tabbar选中的item区域变色
//此处的3不可写self.tabBar.items.count, 会发生回朔错误
self.tabBar.selectionIndicatorImage = [NAAppTools drawTabBarItemBackgroundImageWithItemCount:3];
}
在AppTools中
+ (UIImage *)drawTabBarItemBackgroundImageWithItemCount:(NSInteger)itemCount{
CGSize indicatorImageSize = CGSizeMake(SCREEN_WIDTH / itemCount, TABBAR_HEIGHT);
UIGraphicsBeginImageContext(indicatorImageSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//具体什么颜色在这改, 默认橘黄色
CGContextSetRGBFillColor(ctx, 1.0, 0.84, 0.33, 1);
CGContextFillRect(ctx, CGRectMake(0, 0, indicatorImageSize.width, indicatorImageSize.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
特别鸣谢: http://www.jianshu.com/users/a1123b6fcadb/latest_articles 的作者流年如秋