tabBarItem setBadgeValue 样式自定义(IOS)

1.将自定义样式的提示点转换成为Image

-(UIImage*)convertViewToImage:(UIView*)v{
    CGSize s = v.bounds.size;
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
    [v.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

2.将tabbarItem图片与上面转换的图片进行连接

- (UIImage *)combine:(UIImage*)leftImage rightImage:(UIImage*)rightImage {
    CGFloat width = leftImage.size.width + 10;
    CGFloat height = leftImage.size.height;
    CGSize offScreenSize = CGSizeMake(width, height);
    UIGraphicsBeginImageContextWithOptions(offScreenSize, NO, 0.0);
    
    CGRect rect = CGRectMake(5, 0, leftImage.size.width, leftImage.size.height);
    [leftImage drawInRect:rect];
    
    rect.origin.x += width/2;
    [rightImage drawInRect:CGRectMake(leftImage.size.width - 10, 0, rightImage.size.width, rightImage.size.height)];
    
    UIImage* imagez = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return imagez;
}

3.设置tabBarItem的Image

        [xxxx.tabBarItem setFinishedSelectedImage:[self combine:[UIImage imageNamed:@"icon_tabbar_session_select"] rightImage:[self convertViewToImage:_tipView]] withFinishedUnselectedImage:[self combine:[UIImage imageNamed:@"icon_tabbar_session"] rightImage:[self convertViewToImage:_tipView]]];

效果:

你可能感兴趣的:(tabBarItem setBadgeValue 样式自定义(IOS))