【问题标题】:iOS Tab Bar custom items images, tab bar draws gray images on the leftiOS Tab Bar自定义项目图片,标签栏左侧绘制灰色图片
【发布时间】:2017-10-05 02:11:57
【问题描述】:

我尝试更改标签栏项目图像的颜色,为此我使用下一个代码:

// Generate a tinted unselected image based on image passed via the storyboard.
for (UIViewController *vc in tabBarController.viewControllers) {
    UITabBarItem *item = vc.tabBarItem;
    UIImage *image = item.image;
    UIImage *imageSel = [image imageWithColor:selectedColor];
    UIImage *imageUnsel = [image imageWithColor:unselectedColor];

    // Next is not working to set unselected image! 
    // But setFinishedSelectedImage does.
    //item.selectedImage = imageSel;
    item.image = imageSel;
    //[item setFinishedSelectedImage:imageSel withFinishedUnselectedImage:imageUnsel];
}

UITabBarItem *item = tabBarController.moreNavigationController.tabBarItem;
UIImage *image = [UIImage imageNamed:@"menu-more"];
UIImage *imageSel = [image imageWithColor:selectedColor];
UIImage *imageUnsel = [image imageWithColor:unselectedColor];
item.image = imageSel;
// [item setFinishedSelectedImage:imageSel withFinishedUnselectedImage:imageUnsel];

imageWithColor: 是 UIImage 扩展,使用原始图像的 alpha 值生成带颜色的图像。

代码第一次执行时,一切正常。

更改颜色后(调用上面的代码),所有标签栏项目都显示在标签栏的左侧,并带有文本。发生在模拟器和设备(iPhone + iPad)中。这是一个错误吗?

【问题讨论】:

    标签: ios objective-c iphone uitabbaritem


    【解决方案1】:

    您可以使用情节提要本身将图像设置为 tabbaritem。要将颜色更改为选定的 tabbaritem,无需编写任何 for 循环。您可以使用以下行。

            [[UITabBar appearance] setTintColor:[UIColor greenColor]];
    

    【讨论】:

    • 这只会改变选中项的颜色,但未选中项会显示为灰色。已经试过了。
    • 我想知道你正在为 tabbaritem 图像使用选定和未选定的颜色。
    • 是的,两者都是,实际上未选择的颜色与选择的颜色相同,但只有 0.5 的 alpha。
    【解决方案2】:

    好吧,我想出了解决办法。您需要从头开始创建 UITabBarItem 并在那里设置图像,而不是设置 UITabBarItem 的图像,如下所示:

    for (UIViewController *vc in tabBarController.viewControllers) {
        UITabBarItem *newItem = [[UITabBarItem alloc] initWithTitle:someTitle image:someImageUnsel selectedImage:someImageSel];
        vc.tabBarItem = newItem;
    }
    
    UITabBarItem *newItem = [[UITabBarItem alloc] initWithTitle:someTitle image:someImageUnsel selectedImage:someImageSel];
    tabBarController.moreNavigationController.tabBarItem = newItem;
    

    这个解决方案有一个问题:MoreNavigationController 将在设置它的 tabBarItem 后弹出。普通苹果,wtf?!

    如果有人可以提供更好的解决方案,请...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      相关资源
      最近更新 更多