2

我正在开发一个选项卡式应用程序,我想集成自定义 UITabbar 背景和图标。我已经插入了自定义背景,但图标有问题。

这就是图标的外观:

在此处输入图像描述

但是在情节提要中为每个视图添加图标后,我会得到如下灰色图标:

在此处输入图像描述

有谁知道为什么图标以这种方式显示?

非常感谢!

4

6 回答 6

5

使用 Xcode 6,为了能够直接从 Storyboard 更改图像,您可以这样做:

@IBDesignable class CustomizedTabBarItem: UITabBarItem {
    @IBInspectable var finalImage:UIImage = UIImage() {
        didSet {
            self.image = finalImage.imageWithRenderingMode(.AlwaysOriginal)
        }
    }
    @IBInspectable var finalSelectedImage:UIImage = UIImage() {
        didSet {
            self.selectedImage = finalSelectedImage.imageWithRenderingMode(.AlwaysOriginal)
        }
    }
}

之后,您只需在情节提要中设置图像 在此处输入图像描述

于 2014-12-06T19:37:38.047 回答
2

我还不能发表评论,所以我会添加作为答案。

由于 iOS 7setFinishedSelectedImage:withFinishedUnselectedImage:已弃用。

要解决此问题,您可以这样做:

UIImage *selectedImage = [UIImage imageNamed:@"tabbar-highlight"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedImage = [UIImage imageNamed:@"tabbar"];
unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem = [[UITabBarItem alloc] initWithTitle:@"TabBarItem1" image:unselectedImage selectedImage:selectedImage];
//
//OR
//
UIImage *selectedImage = [UIImage imageNamed:@"tabbar-highlight"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectedImage = [UIImage imageNamed:@"tabbar"];
unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[yourTabBarItem setImage:unselectedImage];
[yourTabBarItem setSelectedImage:selectedImage];

希望这有助于澄清 iOS7+。

于 2014-10-28T17:53:43.813 回答
1

这就是我在我的一个应用程序中执行此操作的方式。在您的 AppDelegatesdidFinishLaunchingWithOptions:方法中,添加此内容并填写您的图像名称:

    UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

    //tab1
    UIImage *selectedImage = [UIImage imageNamed:@"home-tab-selected"];
    UIImage *unselectedImage = [UIImage imageNamed:@"home2-tab"];
    MyTabBar *tabBar = (MyTabBar *)tabController.tabBar;
    UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
    [item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];

    //tab2
    selectedImage = [UIImage imageNamed:@"customers-tab-selected"];
    unselectedImage = [UIImage imageNamed:@"customers-tab"];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
    [item2 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];

    //tab3
    selectedImage = [UIImage imageNamed:@"maps-tab-selected"];
    unselectedImage = [UIImage imageNamed:@"maps-tab"];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:2];
    [item3 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];

    //tab4
    selectedImage = [UIImage imageNamed:@"reports-tab-selected"];
    unselectedImage = [UIImage imageNamed:@"reports-tab"];
    UITabBarItem *item4 = [tabBar.items objectAtIndex:3];
    [item4 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];

    //tab5
    selectedImage = [UIImage imageNamed:@"orders-tab-selected"];
    unselectedImage = [UIImage imageNamed:@"orders-tab"];
    UITabBarItem *item5 = [tabBar.items objectAtIndex:4];
    [item5 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];


    if ([tabBar respondsToSelector:@selector(setBackgroundImage:)])
    {
        // ios 5 code here
        [tabBar setBackgroundImage:[UIImage imageNamed:@"tab-bg"]];

    }   

非常适合我。

于 2013-09-11T13:40:52.787 回答
1

Julien 关于 IBInspectable 的回答的更新版本如下

import UIKit

@IBDesignable
class CustomTabBarItem: UITabBarItem {
    @IBInspectable var finalImage: UIImage = UIImage() {
        didSet {
            self.image = finalImage.withRenderingMode(.alwaysOriginal)
            }
        }
    @IBInspectable var finalSelectedImage: UIImage = UIImage() {
       didSet {
            self.selectedImage = finalSelectedImage.withRenderingMode(.alwaysOriginal)
            }
        }
}
于 2018-01-04T07:32:13.573 回答
0

setFinishedSelectedImage:withFinishedUnselectedImage:您可以使用on设置标签栏图标的完成图像UITabBarItem

在此处查看文档(在外观下查看)

的默认行为UITabBarItem是根据您的图标创建一个蒙版,然后应用特殊的蓝色处理作为您的查看。

于 2013-09-11T13:39:45.670 回答
0

你也可以保持简单;)

    [tabBarItemName setSelectedImage:[UIImage imageNamed:@"image name"]];

您必须在 ViewController.h 中声明 tabBarItemName

于 2014-07-28T09:22:11.900 回答