问题
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
回答1:
As of Xcode 6, you can do this by default in Interface Builder. No need for any custom subclasses or categories as before.
回答2:
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.
回答3:
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.
回答4:
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"]];
回答5:
Use like below and its solve the image issue in iOS7:
[self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"MySelectedIcon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
回答6:
You can user sub method to init a tabBarItem.
-(instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage
来源:https://stackoverflow.com/questions/19426251/selected-state-of-tab-bar-icon-in-ios-7