7

I'm having fun learning to build my first iPhone app and wonder if someone would kindly point me in the right direction.

I have basically added in custom icons for my tab bar (IOS 7). Now I want to add in a custom selected state icon for each of these. How would I do this?

Thanks

Shell

Matthias Bauch
  • 88,097
  • 19
  • 217
  • 244
user2889540
  • 127
  • 1
  • 5

6 Answers6

19

As of Xcode 6, you can do this by default in Interface Builder. No need for any custom subclasses or categories as before.

MrAlek
  • 1,710
  • 14
  • 16
4

On iOS7 you should set selectedImage

tabBarItem.selectedImage = selectedImage;
tabBarItem.image = unselectedImage;

Keep in mind that selectedImage is not available in iOS6.
Use – setFinishedSelectedImage:withFinishedUnselectedImage: if you have to support iOS6.

fishinear
  • 5,674
  • 3
  • 32
  • 80
Matthias Bauch
  • 88,097
  • 19
  • 217
  • 244
4

Here is the swift solution based on @MrAlek's solution, create a custom UITabBarItem

import UIKit

@IBDesignable
class YourTabBarItem: UITabBarItem {

    @IBInspectable var selectedImageName:String!{
        didSet{
            selectedImage = UIImage(named: selectedImageName)
        }
    }
}

and in interface builder, change the class of the tab bar item and you will see the Selected Image Name attribute, just specify your selected image name there. I reckon @IBInspectable is using the runtime attribute.

enter image description here

codingrhythm
  • 1,086
  • 11
  • 24
2

See my more complete answer at https://stackoverflow.com/a/20007782/1755055

Often your tab will have a Navigation Controller stack, so you will need the following

- (void)viewDidLoad
{
    [super viewDidLoad];

...

    [self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"MySelectedIcon.png"]];

}

If you only have one view controller in the tab without the UINavigationController wrapper, you would use

[self.tabBarItem setSelectedImage:[UIImage imageNamed:@"MySelectedIcon.png"]];
Community
  • 1
  • 1
James
  • 811
  • 6
  • 10
1

Use like below and its solve the image issue in iOS7:

[self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"MySelectedIcon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
Marek Lipka
  • 48,573
  • 7
  • 81
  • 87
Sandip Patel - SM
  • 3,143
  • 25
  • 27
-1

You can user sub method to init a tabBarItem.

-(instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage