大小错误setSelectionIndicatorImage

美好的一天! 我把一个图片放在标签栏中,但是它的大小不正确。 帮我解决问题。我想填写整个项目。

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

在这里输入图像说明

我解决了这个问题,你需要正确计算图像;

 + (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; } 

尝试下面的代码

尝试下面的内容:

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

要着色非活动button,将下面的代码放在VC的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]; 

你需要为所有tabBarItems做到这一点,希望这将工作。