简体   繁体   English

无法设置标签栏图标iOS

[英]Can't set tab bar icon iOS

I have a UITabBarController with a tab bar (of course), and I'm trying to set custom images to the tabs on my tab bar with the following code: 我有一个带有标签栏的UITabBarController(当然),并且我试图使用以下代码将自定义图像设置到我的标签栏上的标签中:

- (void)viewDidLoad
{
    [super viewDidLoad];


    UITabBar *menuTab = self.tabBarController.tabBar;

    UIImage *friendImage = [UIImage imageNamed:@"friends_tab@2x.png"];
    UIImage *friendImageSelected = [UIImage imageNamed:@"friends_tab_selected@2x.png"];

    UITabBarItem *friendItem = [menuTab.items objectAtIndex:1];
    [friendItem initWithTitle:@"Friends" image:friendImage selectedImage:friendImageSelected];

}

on the [friendItem initWithTitle:@"Friends" image:friendImage selectedImage:friendImageSelected]; [friendItem initWithTitle:@"Friends" image:friendImage selectedImage:friendImageSelected]; code line, i'm getting a warning "Expression Result unused" and the tab item neither get the image I set nor the text. 代码行,我收到警告“表达式结果未使用”,并且选项卡项既未获取我设置的图像,也未获取文本。

What am I doing wrong? 我究竟做错了什么?

You can't access the items in a UITabBar that way. 您不能以这种方式访问UITabBar中的项目。 Basically, once a UITabBarItem is created, it's read-only. 基本上,一旦创建了UITabBarItem ,它就是只读的。 (The only exceptions to this are badgeValue , selectedImage , and title positioning.) (唯一的例外是badgeValueselectedImage和标题位置。)

Also, you're getting the warning you are because init... methods returns a value, which you're not using. 另外,您将收到警告,因为init...方法将返回一个未使用的值。 Calling init... methods on an existing object is just wrong. 在现有对象上调用init...方法是错误的。

Instead, what you need to do it something like this: 相反,您需要执行以下操作:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIViewController *vc            = self.viewControllers[1];
    UIImage *friendImage            = [UIImage imageNamed:@"friends_tab"];
    UIImage *friendImageSelected    = [UIImage imageNamed:@"friends_tab_selected"];
    UITabBarItem *friendItem        = [[UITabBarItem alloc] initWithTitle:@"Friends" image:friendImage selectedImage:friendImageSelected];
    vc.tabBarItem                   = friendItem;
}

Notice that I create a completely new UITabBarItem . 请注意,我创建了一个全新UITabBarItem When you have a UITabBarController , you are unable to have access to its UITabBar in the same way as one you create and manage yourself. 当您拥有一个UITabBarController ,您将无法像创建和管理自己的UITabBar一样访问其UITabBar Therefore, you need to access the tabBarItem property of the UIViewController you wish to modify. 因此,您需要访问要修改的UIViewControllertabBarItem属性。

You have already initialized your friend item--it was already made and you are retrieving it. 您已经初始化了您的朋友项目-它已经完成并且正在检索它。 You need to remove your initWithImage line andchange the title and image like properties, for example: 您需要删除initWithImage行,并更改标题和图像之类的属性,例如:

[friendItem setImage:@"myImage"];

and you can do the same for the title and the selected image. 并且您可以对标题和所选图像进行相同的操作。 Does this work? 这样行吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM