Wrong size setSelectionIndicatorImage

2019-07-20 08:20发布

问题:

Good day! I put a picture in a tab bar but it has wrong sizes. Help me solve the problem.I want to fill the entire Item.

+ (void)setupTabBarAppearance
{
     [[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:30.0f/255.0f green:201.0f/255.0f blue:224.0f/255.0f alpha:1]];

     UIImage *image = [[self imageWithColor:[UIColor colorWithRed:255.0f/255.0f green:198.0f/255.0f blue:25.0f/255.0f alpha:1]] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 1, 2) resizingMode:UIImageResizingModeStretch];
     [[UITabBar appearance] setSelectionIndicatorImage:image];
}

回答1:

I solved this problem you need to properly calculate image;

+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height); //  <- Here
    // Create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);   // Fill it with your color
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}


回答2:

Try the below code

Try the below:

  [[UITabBar appearance] setTintColor:[UIColor redColor]];
  [[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];

To tint the non active buttons, put the below code in your VC's viewDidLoad:

  UITabBarItem *tabBarItem = [yourTabBarController.tabBar objectAtIndex:0];
  UIImage *unselectedImage = [UIImage imageNamed:@"icon-unselected"];
  UIImage *selectedImage = [UIImage imageNamed:@"icon-selected"];
  [tabBarItem setImage: [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  [tabBarItem setSelectedImage: selectedImage];

You need to do this for all the tabBarItems, Hope this will work.