简体   繁体   English

使用故事板更改 UItabbar 项目的图像

[英]Change Image of UItabbar Item, Using storyboards

I have the following story board:我有以下故事板:

在此处输入图片说明

As you can see there is a tab bar application with 5 tabs, on the storyboard I've assign the logo for each tab.如您所见,有一个带有 5 个选项卡的选项卡栏应用程序,在情节提要上,我为每个选项卡分配了徽标。 Now when the user clicks a cell in a particular view I want to change the image of one of the tabs.现在,当用户单击特定视图中的单元格时,我想更改其中一个选项卡的图像。 How can I do this?我怎样才能做到这一点? I don't have an instance of the tab bar view controller or items since storyboards pretty much does all this for me.我没有标签栏视图控制器或项目的实例,因为故事板几乎为我完成了所有这些工作。 So my question is what methods do I have to implement to change the image?所以我的问题是我必须实施哪些方法来更改图像? If I need the tab bar controller how can I get its instance and in which class should I point it to?如果我需要标签栏控制器,我如何获取它的实例以及我应该将它指向哪个类?

Thank you very much,非常感谢你,

In any UIViewController class that is part of the Tab Bar hierarchy, all you have to do to get in instance of the tab bar controller is:在属于 Tab Bar 层次结构的任何UIViewController类中,要获得 Tab Bar 控制器的实例,您所需要做的就是:

//In UIViewController
UITabBarController *tabBarController = self.tabBarController;

You can then change the image as so然后您可以更改图像

//Suppose you want to change the 1st (0th) tab bar image
UITabBarItem * tabItem = [tabBarController.tabBar.items objectAtIndex: 0];
tabItem.image = //whatever image you want to change to

Each UIViewController has a property called tabBarItem which is a UITabBarItem that the tab bar controller uses to set the image representing that controller.每个 UIViewController 都有一个名为tabBarItem的属性,它是一个UITabBarItem ,选项卡栏控制器使用它来设置表示该控制器的图像。 You can manipulate that to change the image of the controller in question.您可以操纵它来更改相关控制器的图像。

I found that -- at least in Xcode 6.1.1 using Swift -- the direct manipulation of the tabBarItem did not work for me.我发现——至少在使用 Swift 的 Xcode 6.1.1 中——直接操作 tabBarItem 对我不起作用。

However, @borrrden's answer put me on the right track.然而,@borrrden 的回答让我走上了正轨。 Apple's documentation for UITabBarController states pretty clearly: Apple 的 UITabBarController 文档非常清楚地说明:

You should never access the tab bar view of a tab bar controller directly.您永远不应该直接访问选项卡栏控制器的选项卡栏视图。 To configure the tabs of a tab bar controller, you assign the view controllers that provide the root view for each tab to the viewControllers property.要配置选项卡栏控制器的选项卡,请将为每个选项卡提供根视图的视图控制器分配给 viewControllers 属性。

... ...

Tab bar items are configured through their corresponding view controller.标签栏项目通过其相应的视图控制器进行配置。 To associate a tab bar item with a view controller, create a new instance of the UITabBarItem class, configure it appropriately for the view controller, and assign it to the view controller's tabBarItem property.要将选项卡栏项与视图控制器关联,请创建 UITabBarItem 类的新实例,为视图控制器适当配置它,并将其分配给视图控制器的 tabBarItem 属性。

Therefore, in accordance with that, below is what I came up with that did work for me.因此,据此,以下是我想出的对我有用的内容。

It's written in Swift, and I hope that future readers can translate it accordingly if they need to (I also changed the image names to be super-generic).它是用 Swift 编写的,我希望未来的读者可以根据需要进行相应的翻译(我还将图像名称更改为超级通用)。

I also used UIImage's imageWithRenderingMode method so I could use custom images instead of the shadowy silhouette default images that iOS creates (I would like to credit @NSHeffalump's answer here for that...).我还使用了 UIImage 的 imageWithRenderingMode 方法,因此我可以使用自定义图像而不是 iOS 创建的阴影轮廓默认图像(我想在这里归功于 @NSHeffalump 的回答......)。

    if let viewControllers = tabBarController.viewControllers as? Array<UIViewController> {
        var tabBarItemImageNames = ["TabBarItemImage0","TabBarItemImage1","TabBarItemImage2","TabBarItemImage3","TabBarItemImage4"]
        var vcIndex = 0
        for vc:UIViewController in viewControllers {
            let selectedImage = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            let image = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            var tabBarItem = UITabBarItem(title: "", image: image, selectedImage: selectedImage)
            vc.tabBarItem = tabBarItem
            vcIndex++
        }
    }

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

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