Setting Selected Image in Tab Bar Controller with Storyboard
Solution 1
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.
I am using XCode 6.0 and my minimum deployment target is iOS 8.0.
Solution 2
Here is complete solution for selected/unselected image in Tabbar For XCode >= 8:
- Go to Image assets -> select image
- Select Render AS: "Original Image"
- After that go to storyboard -> Select Tabbar Item
- Under Attribute Inspectors, set "Selected Image" & "Image" as shown in following screenshot, that`s it:
Solution 3
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 :)
Solution 4
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];
Solution 5
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.
Siddharth
A hacker working currently in ShopSocially. Also a freelance hacker developing in Python and PHP but always on the lookout for better languages and better frameworks. Email me at sidchilling[at]gmail[dot]com if you want something done :)
Updated on July 08, 2022Comments
-
Siddharth almost 2 years
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.
-
Steve almost 10 yearsThere is no class called
UINavigationViewController
and if you meantUINavigationController
, it has no property calledtabBar
. -
Steve almost 10 yearsThis should be the accepted answer, noting that the "corresponding view controller" is the
UINavigationController
's root view controller. -
CTiPKA almost 10 yearsThe actual first line of code should be like:
UITabBar *tabBar = self.navigationController.tabBarController.tabBar;
-
Code Commander over 9 yearsThe new version of XCode has a "Selected Image" field in the Attributes Inspector but it doesn't seem to work. Thankfully this answer worked for me.
-
Jake Lisby over 9 yearsThis is great, I've been searching for how to fix this for hours. Thanks for providing the answer!
-
Mansi Panchal over 9 yearsI have tried same. But, not working for me. I am not sure what is reason.
-
mattorb over 9 yearsSame issue. This worked. The built in UI for IB did not. 2-16-2015 Xcode 6.1.1
-
Trianna Brannon almost 9 years@DavidNelson you can only fill in the image section, you must leave the selected image section empty for it to work.
-
Eric G almost 9 yearsAs of Xcode 6.4 and August 2015, this bug is still affecting Xcode. Amazing. Thanks for this answer!
-
Marek Stój almost 9 yearsThanks. If it's not working, one might want to try cleaning first.
-
wcochran almost 9 yearsWhat 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.
-
sudoExclaimationExclaimation over 8 yearsTesting on Xcode 7 - the Selected Image field in the Attributes Inspector of UITabBarItem seems to work fine for me now. Testing in iOS 9. Not sure if it will still work on iOS 8 devices though. EDIT: Tested in 8.4 simulator - works there too with the selected image field in storyboard
-
IamMashed over 8 yearsI 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. ?
-
Loebre over 8 yearsNow you can use from the Inspector the Bar Item -> Image field, watch out NOT the Tab Bar Item -> Selected Image.
-
Tiago Mendes about 7 yearsthe xcode (8.3.1) is crashing every time that i try to add
-
Samman Bikram Thapa over 6 yearsWorks. Thanks in xcode (9.3.2) for swift 4 (deployed for iOS 10+)
-
mike.tihonchik over 6 yearsThis 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
-
Karthik Kannan over 6 yearsHi! 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
-
Ethan Parker about 6 yearsWas very confused on how to set the default tab, thanks for showing this.
-
Tiago Mendes about 6 yearsI'm glad I could help :D
-
Reinhard Männer about 6 yearsThis worked for me. Actually, it was not necessary to set Render AS: "Original Image", the default value did it also.
-
andrewlundy over 4 yearsIn Xcode 11, I've found that you need to use the image attribute that is under
Bar Item
in the attribute inspector.