设置特定标签栏项目的背景颜色并使其成为一个单独的项目

Setting the background color of a specific tab bar Item and making it a seperate item

就像 Instagram 应用程序一样,我想让我的中心标签栏项目具有特定的颜色。

我知道这是我设置所选图像的方式:

[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:0.484 green:0.403 blue:0.884 alpha:1] forSize:CGSizeMake(64, 49) withCornerRadius:0]];

但是我怎样才能使第 3 个标签的背景始终保持该颜色?

此外,如何使中间选项卡与其余选项卡分开,例如,当我按下中心选项卡时,会出现视图控制器(如 instagram)的模态显示,并且上一个选项卡保持选中状态关闭模态视图控制器。

@interface BaseViewController : UITabBarController

// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;

// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:  (UIImage*)highlightImage;


@implementation BaseViewController


-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image
{
UIViewController* viewController = [[UIViewController alloc] init] ;
 viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];

return viewController;
 }

// 创建一个自定义的 UIButton 并将其添加到我们的标签栏的中心

  -(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
 button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
 [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];

CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
  button.center = self.tabBar.center;
else
 {
center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
 }

  [self.view addSubview:button];
 }

将此标签控制器子类化并调用上述函数来设置标签栏项目和中心按钮的图像,如 Instagram

  @interface InstagramViewController : BaseViewController


  @end

@implementation InstagramViewController

- (void)viewDidLoad
   {
      [super viewDidLoad];

 self.viewControllers = [NSArray arrayWithObjects:
                        [self viewControllerWithTabTitle:@"Feed" image:[UIImage imageNamed:@"112-group.png"]],
                        [self viewControllerWithTabTitle:@"Popular" image:[UIImage imageNamed:@"29-heart.png"]],
                        [self viewControllerWithTabTitle:@"Share" image:nil],
                        [self viewControllerWithTabTitle:@"News" image:[UIImage imageNamed:@"news.png"]],
                        [self viewControllerWithTabTitle:@"@user" image:[UIImage imageNamed:@"123-id-card.png"]], nil];
}


 -(void)willAppearIn:(UINavigationController *)navigationController
    {

     [self addCenterButtonWithImage:[UIImage imageNamed:@"cameraTabBarItem.png"] highlightImage:nil];
    }

@end