好,這裏是我發現的是填充使用可調整大小的圖像時纔會出現。當使用不可調整大小的圖像時,填充不在那裏。
因此,一個可能的解決方案是子類UITabBar並配置項目大小更改時selectionIndicatorImage
。
@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
太棒了!每邊2px對iOS 7來說已經足夠了,否則項目圖標會移向邊緣。 – hybridcattt 2014-02-05 07:57:48