开发者

MoreNavigationController images disappearing on select

开发者 https://www.devze.com 2023-01-16 23:37 出处:网络
I have a UITabBarController which has been created programatically, which has 6 tabs. As such the MoreNavigationController is automatically created to take care of having more than 5 tabs. Everything

I have a UITabBarController which has been created programatically, which has 6 tabs. As such the MoreNavigationController is automatically created to take care of having more than 5 tabs. Everything looks fine when the MoreNavigationController is displayed, but when I select one of these rows to push the view controller on to the stack, the cell image (tab bar image) disappears. When I pop that view controller, the image remains hidden until the pop animation is completed, at which point the image suddenly appears again.

This is fairly old code and I wouldn't do it this way these days, but everything works except for this last little thing so I'm pretty hesitant to rip out all the code and do it another way. Can anyone suggest what I might be doing wrong?

An example of creating one of the tab bar view controllers:

InfoViewController* infoViewController = [[InfoViewController alloc] init];
infoViewController.tabBarItem.image = [UIImage imageNamed:@"90-life-buoy.png"];
infoViewController.tabBarItem.title = @"More Info";
infoViewController.title = @"More Info";
UINavigationController* infoNavController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
[infoViewController release];

Creating the tab bar:

tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:outdoorsNavController, peopleNavController, citiesNavController, landscapesNavController, infoNavController, basicsNavController, nil];
[window addSubview:tabBarController.view];

EDIT: Doesn't seem to make any difference whether 开发者_JAVA技巧I use retina (@2x) images or not.


The issue is because you're wrapping your InfoViewController in a UINavigationController.

When you click on the table row in MoreNavigationController, the controller uses the tabBarItem in UINavigationController while it does its transition. Because this is nil (in your code), the image in MoreNavigationController disappears. When the transition finally finishes, MoreNavigationController picks up the tabBarItem in InfoViewController

Try this:

InfoViewController* infoViewController = [[InfoViewController alloc] init];
infoViewController.tabBarItem.image = [UIImage imageNamed:@"90-life-buoy.png"];
infoViewController.tabBarItem.title = @"More Info";
infoViewController.title = @"More Info";
UINavigationController* infoNavController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
//Set the tabBarItem for UINavigationController
infoNavController.tabBarItem = infoViewController.tabBarItem
[infoViewController release];

Here's a video reproducing and fixing the issue: Item 7 has an empty tabBarItem.image while Item 6 has tabBarItem.image set

MoreNavigationController images disappearing on select


I'm not sure, but have you tried setting the NavigationCotroller's .tabBarItem?


Not sure if I understand correctly, but on the top of my head here are some suggestions (The More Tab Bar item shouldn't disappear at all times, that is created automatically)

  1. Have you tried subclassing your UINavigationController that is being used for the MoreController's place and set its tab bar items properties there, therefore making sure you have control over its lifetime ?

  2. Subclass your UIViewController's that you wish to push onto the navigation stack and have them use the same tab bar item ?

  3. Mimic the default functionality, create your own UITableViewController to act as the More Controller, and at each tap of the rows, do what you want, also in a custom way.

PS: Try setting images name without the .png extension. This way you will automatically load the @2x resource as well. Eg: [UIImage imageNamed:@"90-life-buoy"]

Link

0

精彩评论

暂无评论...
验证码 换一张
取 消