如何删除UITabBarItem selectionImage填充?

我通过UITabBar外观将SelectionIndicatorImage设置为可伸缩图像(以适应各种设备宽度):

UIImage *selectedImage = [[UIImage imageNamed:@"SelectedTab"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [[UITabBar appearance] setSelectionIndicatorImage:selectedImage]; 

结果我在屏幕边缘上得到了一个2pt填充。 我基本上试图使用选择指标作为当前所选UITabBarItem的背景。

http://sofzh.miximages.com/ios/qFHEk.png

什么是解决这个问题的简单方法?

如果你想在你的应用程序中使用具有选择和取消选择function的UITabBarController,那么你应该使用这个代码

 [[self.tabBarController tabBar] setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_tabBar" ofType:@"png"]]]; [[self.tabBarController tabBar] setSelectionIndicatorImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_tabItem_selected" ofType:@"png"]]]; NSArray *arrTabItems = [[self.tabBarController tabBar] items]; UITabBarItem *tabBarItem = [arrTabItems objectAtIndex:0]; [tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"img_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"img_unselected.png"]]; [tabBarItem setImageInsets:UIEdgeInsetsMake(5, 5, 0, 5)]; 

我尝试过类似的东西。 结束这种方式:

子类UITabBarController并添加属性:

 @property(nonatomic, readonly) UIImageView *imageView; 

根据需要设置图像:

 - (void)viewDidLoad { [super viewDidLoad]; _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbaritem"]]; [self.tabBar addSubview:self.imageView]; } 

将您的子视图放在正确的位置:

 - (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))]; } 

然后,您只需要在每个选项卡更改时刷新布局:

 - (void)setSelectedIndex:(NSUInteger)selectedIndex { [super setSelectedIndex:selectedIndex]; [self.view setNeedsLayout]; } - (void)setSelectedViewController:(UIViewController *)selectedViewController { [super setSelectedViewController:selectedViewController]; [self.view setNeedsLayout]; } 

旧的回答如何删除填充(这是我首先理解为你的问题,我将其作为其他人的参考)。

在iOS 7+中, UITabBar具有以下属性:

 /* 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; 

这应该可以让你摆脱项之间的填充。