Well, here what I've found is that the padding appears only when using resizable images. When using non resizable images, the padding is not there.
Therefore, a possible solution is to subclass UITabBar and configure the selectionIndicatorImage
whenever the item size changes.
@interface TKTabBar
@end
@implementation TKTabBar
{
CGSize _selectionIndicatorImageSize;
}
- (void)tk_refreshSelectionIndicatorImageForItemSize:(CGSize)itemSize
{
// Recompute the selection indicator image only if the size of the item has changed.
if (!CGSizeEqualToSize(itemSize, _selectionIndicatorImageSize))
{
_selectionIndicatorImageSize = itemSize;
// Compute here the new image from the item size.
// In this example I'm using a Cocoa Pod called UIImage+Additions to generate images dynamically.
UIImage *redImage = [UIImage add_imageWithColor:[UIColor add_colorWithRed255:208 green255:75 blue255:43] size:CGSizeMake(itemSize.width, 2)];
UIImage *clearImage = [UIImage add_imageWithColor:[UIColor clearColor] size:CGSizeMake(itemSize.width, itemSize.height)];
UIImage *mixImage = [clearImage add_imageAddingImage:redImage offset:CGPointMake(0, itemSize.height-2)];
// Finally, I'm setting the image as the selection indicator image.
[self setSelectionIndicatorImage:mixImage];
}
}
// Using the layout subviews method to detect changes on the tab size
- (void)layoutSubviews
{
[super layoutSubviews];
// Only needed if at least one item
if (self.items.count > 0)
{
CGSize itemSize = CGSizeZero;
// Iterating over all subviews
for (UIView *view1 in self.subviews)
{
// Searching for "UITabBarButtons"
if ([view1 isKindOfClass:NSClassFromString(@"UITabBarButton")])
{
itemSize = view1.bounds.size;
break;
}
}
// Applying the new item size
[self tk_refreshSelectionIndicatorImageForItemSize:itemSize];
}
}
@end