Reload / Refresh tab bar items in a ViewController ?

痞子三分冷 提交于 2020-01-14 10:30:08

问题


I am trying to change images of my tabbar in a ViewController, but to display the new images, I must click on each tab bar item.

for (CustomTabBarItem *myItem in self.tabBarController.tabBar.items){
        myItem.enabled = YES; 

        myItem.badgeValue = @"1"; 

        UIImage *myImage =  [[UIImage alloc] initWithContentsOfFile:[[DesignManager sharedManager] getPathOfFile:@"test.png"]];

        *myItem.imageSelect= *myImage; // change images of each item. don't appear if I dont click on the item
}

Anyone know How can I can display directly these images? Thanks


回答1:


You need to replace the old tab bar item with a new one. You can't update the image dynamically otherwise.

The easiest way to do this is to set the tabBarItem property of the view-controller represented by a given tab. If you wanted to do this from within that view controller, just write:

self.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];

Or, you could do this from somewhere else, say your app delegate:

UIViewController* vc = [tabBarController.viewControllers objectAtIndex: 3];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];



回答2:


I know this is an old question. I ran into the same problem when I need to update the badge value from another active tab. Creating another UITabBarItem will solve your current problem but causes potential memory leak when this code is called many times. Plus, when other view controllers access the tab, they do not have reference to newly created UITabBarItem. My trick is

vc.tabBarItem = vc.tabBarItem; 

It works for me.



来源:https://stackoverflow.com/questions/4216179/reload-refresh-tab-bar-items-in-a-viewcontroller

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