Selected state of tab bar icon in IOS 7

寵の児 提交于 2019-12-03 04:16:41

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

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.

Matthias Bauch

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.

James

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"]];
Sandip Patel - SM

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

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

You can user sub method to init a tabBarItem.

-(instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!