I've tried something similar. Ended up this way:
Subclass UITabBarController
and add property:
@property(nonatomic, readonly) UIImageView *imageView;
Setup your image as you want:
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbaritem"]];
[self.tabBar addSubview:self.imageView];
}
Place your subview in correct position:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// get all UITabBarButton views
NSMutableArray *tabViews = [NSMutableArray new];
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:[UIControl class]]) {
[tabViews addObject:[NSValue valueWithCGRect:view.frame]];
}
}
// sort them from left to right
NSArray *sortedArray = [tabViews sortedArrayUsingComparator:^NSComparisonResult(NSValue *firstValue, NSValue *secondValue) {
CGRect firstRect = [firstValue CGRectValue];
CGRect secondRect = [secondValue CGRectValue];
return CGRectGetMinX(firstRect) > CGRectGetMinX(secondRect);
}];
// place your imageView on selected index button frame
CGRect frame = self.tabBar.bounds;
CGSize imageSize = CGSizeMake(CGRectGetWidth(frame) / self.tabBar.items.count, self.imageView.image.size.height);
CGRect selectedRect = sortedArray.count > self.selectedIndex ? [sortedArray[self.selectedIndex] CGRectValue] : CGRectZero;
[self.imageView setFrame:CGRectIntegral(CGRectMake(CGRectGetMinX(selectedRect), CGRectGetMaxY(frame) - imageSize.height,
CGRectGetWidth(selectedRect), imageSize.height))];
}
Then you only need to refresh layout on each tab change:
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
[super setSelectedIndex:selectedIndex];
[self.view setNeedsLayout];
}
- (void)setSelectedViewController:(UIViewController *)selectedViewController {
[super setSelectedViewController:selectedViewController];
[self.view setNeedsLayout];
}
Old answer how to remove padding (this is what I firstly understood as your question, I'm leaving it as a reference for other people).
In iOS 7+ UITabBar
has following property:
/*
Set the itemSpacing to a positive value to be used between tab bar items
when they are positioned as a centered group.
Default of 0 or values less than 0 will be interpreted as a system-defined spacing.
*/
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
This should enable you to get rid of padding between items.