简体   繁体   English

使用故事板在标签栏控制器中设置选定的图像

[英]Setting Selected Image in Tab Bar Controller with Storyboard

I am using Storyboarding and I have a tab bar controller with five tabs. 我正在使用Storyboarding,我有一个带有五个标签的标签栏控制器。 In the storyboard, I am able to set the image for the tab bar item. 在故事板中,我可以设置标签栏项目的图像。 Apple docs suggest to have two icons for each tab bar item - one for selected and one for the unselected state. Apple文档建议每个标签栏项目有两个图标 - 一个用于选定状态,另一个用于未选择状态。

I am not able to figure out how I can do this using storyboard. 我无法弄清楚如何使用故事板来做到这一点。

You can use storyboard to set selected image of tabbar. 您可以使用故事板设置选定的tabbar图像。 I tried and it worked for me. 我试过,它对我有用。 Select the UITabbarItem and add a run-time attribute 'selectedImage', select Type as 'Image' and give the name of your image as its value. 选择UITabbarItem并添加运行时属性'selectedImage',选择Type作为'Image'并将图像的名称作为其值。

使用故事板设置Tabbar的选定图像

I am using XCode 6.0 and my minimum deployment target is iOS 8.0. 我使用的是XCode 6.0,我的最低部署目标是iOS 8.0。

Here is complete solution for selected/unselected image in Tabbar For XCode >= 8: 以下是Tabbar For XCode> = 8中选定/未选定图像的完整解决方案:

  • Go to Image assets -> select image 转到图像资源 - >选择图像
  • Select Render AS: "Original Image" 选择渲染AS:“原始图像”

在此输入图像描述

  • After that go to storyboard -> Select Tabbar Item 之后转到故事板 - >选择Tabbar项目
  • Under Attribute Inspectors, set "Selected Image" & "Image" as shown in following screenshot, that`s it: 在Attribute Inspectors下,设置“Selected Image”和“Image”,如下面的屏幕截图所示:

在此输入图像描述

In XCode 8 and above you can simply do it in Image assets,Just select the image and select Render as "Original Image". 在XCode 8及更高版本中,您只需在图像资源中执行此操作,只需选择图像并选择渲染为“原始图像”即可。 (Please check attached image)..Have FUN :) (请查看附图)..有乐趣:) 在此输入图像描述

Yes, this cannot be done using storyboards - needs code to be written. 是的,这不能使用故事板来完成 - 需要编写代码。

In the UINavigationViewController 's viewDidLoad method, we can write the following code - UINavigationViewControllerviewDidLoad方法中,我们可以编写以下代码 -

UITabBar *tabBar = self.tabBar;

UITabBarItem *targetTabBarItem = [[tabbar items] objectAtIndex:0]; // whichever tab-item
UIImage *selectedIcon = [UIImage imageNamed:@"name-of-selected-image.png"];
[targetTabBarItem setSelectedImage:selectedIcon];

You can now do this easily in storyboard. 您现在可以在故事板中轻松完成此操作。 On each tabviewcontroller that you have, it should contain a Tab Bar Item in the hierarchy (looks like a little blue star), Click on this and the settings on the right should look like the image below. 在您拥有的每个tabview控制器上,它应该在层次结构中包含一个Tab Bar项(看起来像一个小蓝星),单击此按钮,右侧的设置应如下图所示。 The tab bar title & image can be changed here. 标签栏标题和图像可以在此处更改。

在此输入图像描述

In the new Xcode 8 you can do in Storyboard without needing to defined the runtime attributes as is suggested in the high pointed answer. 在新的Xcode 8中,您可以在Storyboard中进行操作,而无需像高级答案中所建议的那样定义运行时属性。

打印屏幕选项卡栏项目

Don't forget that image should have this size: 不要忘记图像应该具有以下尺寸:

  • @1x : about 25 x 25 @ 1x:约25 x 25
  • @2x : about 50 x 50 @ 2x:约50 x 50
  • @3x : about 75 x 75 @ 3x:约75 x 75

I think that the easiest way is setting the image from the Inspector. 我认为最简单的方法是从Inspector设置图像。 you have a field called Bar Item -> Image , and it is in there where you have to set the image name. 你有一个名为Bar Item - > Image的字段,你必须在那里设置图像名称。 Watch out, not confuse with the Tab Bar Item -> Selected Image 注意,不要与标签栏项目 - >选定图像混淆

在此输入图像描述

The icon should be set in the corresponding view controller. 应在相应的视图控制器中设置图标。 When doing this you're free to rearrange the order of view controllers inside the storyboards main tab controller without having to change the code ( objectAtIndex:0 ) for each and every icon. 执行此操作时,您可以自由重新排列故事板主选项卡控制器内的视图控制器的顺序,而无需为每个图标更改代码( objectAtIndex:0 )。

Put the following line into the viewDidLoad method: viewDidLoad下行放入viewDidLoad方法:

 if (self.navigationController.viewControllers.count < 2)
     self.navigationController.tabBarItem.selectedImage = [UIImage imageNamed:@"image-selected.png"];

The if condition makes sure that the button is only changed for the top most view controller. if条件确保仅为最顶层的视图控制器更改按钮。 This is required when you reuse view controllers in a navigation hierarchy as sub view controllers. 将导航层次结构中的视图控制器重用为子视图控制器时,这是必需的。

SWIFT 3.0 --> The Ideal way to set your tab bar button images is as follows : SWIFT 3.0 - >设置标签栏按钮图像的理想方法如下:

first set the images you want to use for the button : 首先设置要用于按钮的图像:

    let homeImage = UIImage(named: "TabHome")
    let homeTappedImage = UIImage(named: "TabHomeRed")

then set the button of type UITabButtonItem : 然后设置UITabButtonItem类型的按钮:

    let homeButton = UITabBarItem(title: homeText, image: homeImage, selectedImage: homeTappedImage)

    //with this method you set the image when the button is not selected 
    homeButton.image = homeImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)


    //with this method you set the image when the button is selected 
    homeButton.selectedImage = homeTappedImage?.withRenderingMode(.alwaysOriginal)

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

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