Reputation: 127
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
Upvotes: 7
Views: 8845
Reputation: 1710
As of Xcode 6, you can do this by default in Interface Builder. No need for any custom subclasses or categories as before.
Upvotes: 19
Reputation: 90117
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.
Upvotes: 4
Reputation: 1546
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.
Upvotes: 4
Reputation: 1
You can user sub method to init a tabBarItem.
-(instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage
Upvotes: -1
Reputation: 3394
Use like below and its solve the image issue in iOS7:
[self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"MySelectedIcon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
Upvotes: 1
Reputation: 861
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"]];
Upvotes: 2