63

I am using Storyboarding and I have a tab bar controller with five tabs. 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.

I am not able to figure out how I can do this using storyboard.

Siddharth
  • 5,009
  • 11
  • 49
  • 71
  • 1
    Simple answer to a straightforward question: You can't, it's code only (for the time being at least). `initWithTitle:image:selectedImage:` – T. Benjamin Larsen Jan 27 '14 at 17:29

9 Answers9

150

You can use storyboard to set selected image of 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.

Setting selected image of Tabbar using storyboard

I am using XCode 6.0 and my minimum deployment target is iOS 8.0.

Hamza Azad
  • 2,637
  • 1
  • 20
  • 15
29

Here is complete solution for selected/unselected image in Tabbar For XCode >= 8:

  • Go to Image assets -> select image
  • Select Render AS: "Original Image"

enter image description here

  • After that go to storyboard -> Select Tabbar Item
  • Under Attribute Inspectors, set "Selected Image" & "Image" as shown in following screenshot, that`s it:

enter image description here

Sagar Sukode
  • 1,361
  • 12
  • 23
17

In XCode 8 and above you can simply do it in Image assets,Just select the image and select Render as "Original Image". (Please check attached image)..Have FUN :)enter image description here

Infaz
  • 1,133
  • 9
  • 19
  • 2
    This is a VERY IMPORTANT piece, as simply setting the UIImage to the selectedImage did nothing for me. (I must note, I was doing it programmatically) Thanks @Infaz – mike.tihonchik Mar 07 '18 at 19:59
12

Yes, this cannot be done using storyboards - needs code to be written.

In the UINavigationViewController's viewDidLoad method, we can write the following code -

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];
Siddharth
  • 5,009
  • 11
  • 49
  • 71
  • 1
    There is no class called `UINavigationViewController` and if you meant `UINavigationController`, it has no property called `tabBar`. – Steve Jul 27 '14 at 09:25
  • 1
    The actual first line of code should be like: `UITabBar *tabBar = self.navigationController.tabBarController.tabBar;` – CTiPKA Sep 14 '14 at 17:50
7

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. The tab bar title & image can be changed here.

enter image description here

Trianna Brannon
  • 1,246
  • 11
  • 12
  • 1
    @DavidNelson you can only fill in the image section, you must leave the selected image section empty for it to work. – Trianna Brannon Jul 01 '15 at 04:23
  • 1
    What is the "Selected Image" for in this case? Why is there a separate section for "Tab Bar Item" and "Bar Item"? If I select one of the system items it works fine -- custom images fail. – wcochran Aug 29 '15 at 15:24
  • 1
    I am getting blue square in place of the image. More details please on how to get it work? I am using xcode 7.1. May be there are some constraints? height width etc. ? – IamMashed Oct 31 '15 at 20:38
  • Hi! I'm getting a colored square in the place of the custom image i want when the tab bar is selected. This happens when I add a selected image using the storyboard as shown in your answer. Any idea why this is happening? I'm using Xcode 9.2 – Karthik Kannan Mar 15 '18 at 10:41
6

I think that the easiest way is setting the image from the Inspector. you have a field called Bar Item -> Image, and it is in there where you have to set the image name. Watch out, not confuse with the Tab Bar Item -> Selected Image

enter image description here

Loebre
  • 615
  • 2
  • 8
  • 23
6

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.

Print Screen Tab Bar item

Don't forget that image should have this size:

  • @1x : about 25 x 25
  • @2x : about 50 x 50
  • @3x : about 75 x 75
Tiago Mendes
  • 4,572
  • 1
  • 29
  • 35
6

SWIFT 3.0 --> The Ideal way to set your tab bar button images is as follows :

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 :

    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)
MhmdRizk
  • 1,591
  • 2
  • 18
  • 34
3

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.

Put the following line into the viewDidLoad method:

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

The ifcondition makes sure that the button is only changed for the top most view controller. This is required when you reuse view controllers in a navigation hierarchy as sub view controllers.

krizzzn
  • 1,413
  • 11
  • 13
  • 1
    This should be the accepted answer, noting that the "corresponding view controller" is the `UINavigationController`'s root view controller. – Steve Jul 27 '14 at 09:30