How can I remove the blue square that overlays UITabBarItem when selected?

谁都会走 提交于 2019-12-02 10:52:33

The images you use for a tab bar item have to have their renderingMode be, UIImageRenderingModeAlwaysOriginal or they will appear as blue squares (templates). The document called "Tab Bars", says this,

Tab Bar Item Icons

Each item in a tab bar can have a custom selected image and unselected image. You can specify these images when you initialize a tab bar item using the initWithTitle:image:selectedImage: method. Note that a tab bar item image will be automatically rendered as a template image within a tab bar, unless you explicitly set its rendering mode to UIImageRenderingModeAlwaysOriginal. For more information, see Template Images.

I don't think you can set them up in the storyboard, so you should do it in the controller's init method,

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        UIImage *img = [UIImage imageNamed:@"pic.jpg"];
        img  = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.tabBarItem setSelectedImage:img];
    }
    return self;
}

This is a Xcode error that will be fixed in 8.2 release. This is the Apple official note about this problem.Look in the link for Interface Builder > Resolved Issues > UITabBarController https://developer.apple.com/library/prerelease/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html

Swift 3+ version of rdelmar's code:

class CustomTabBarController: UITabBarController {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let img = UIImage(imageLiteralResourceName: "pic").withRenderingMode(.alwaysOriginal)
        tabBarItem.selectedImage = img
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!