Alex Rivera | Logout

UITabBar selectionIndicatorImage padding

Asked 2012-04-05T20:11:09.657
20

I set a custom indicator image for my UITabBar like this

UIImage *tabBarSelectedImage = [[UIImage imageNamed:@"tabBar_selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setSelectionIndicatorImage:tabBarSelectedImage];

and get a 4px padding around my tabBarSelectedImage. Is it possible to set that padding to 0px? So that my tabBarSelectedImage fills the entire space and no border is visible?

Edit
Report

1 Answer

5

Here is the solution of your problem... I was not actually doing this...I was doing something else but the following code will help you a lot....First I tell you what I did...

  1. I made a catagory of UITabbar and implement the following method in that

    - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor 
    shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
    {
    CGColorRef cgColor = [color CGColor];
    CGColorRef cgShadowColor = [shadowColor CGColor];
    for (UITabBarItem *item in [self items]) {
    
    if ([item respondsToSelector:@selector(selectedImage)] &&
        [item respondsToSelector:@selector(setSelectedImage:)] &&
        [item respondsToSelector:@selector(_updateView)])
    {
        CGRect contextRect;
        contextRect.origin.x = 0.0f;
        contextRect.origin.y = 0.0f;
    
        //instead of following line you can give our own desired size of contextRect. 
        //just change the method parameters and include a parameter of desired size in it.
        // and this desired size would be the tabbarbutton size...so you will pass the size of 
        // you tabbarbutton here...because on the back of image there is a tabbarbutton and if
        // set the image of button size it will occupy whole the are of button.
    
        contextRect.size = desired size //[[item selectedImage] size];
        // Retrieve source image and begin image context
        UIImage *itemImage = [item image];
        CGSize itemImageSize = [itemImage size];
    
        CGPoint itemImagePosition; 
        itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
        itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
        UIGraphicsBeginImageContext(contextRect.size);
        CGContextRef c = UIGraphicsGetCurrentContext();
    
        // Setup shadow
        CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
    
        // Setup transparency layer and clip to mask
        CGContextBeginTransparencyLayer(c, NULL);
     
answered 2012-11-13T12:43:05.293

Your Answer