Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBar MoreViewController image in UITableView disappears after programmatically selecting it

I'm trying to select one of my UINavigationControllers in the UITabBar.viewControllers array.

I previously tried it with setting the UITabbarController.selectedIndex, but the Apple documentation says: "To select the More navigation controller itself, you must change the value of the selectedViewController property instead."

AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UINavigationController *navController = [appDelegate.objTabBarController.viewControllers objectAtIndex:5];
[appDelegate.objTabBarController setSelectedViewController:navController];

Doing so is fine but when I change back to the MoreViewController's list view, the icon on the left side is gone and won't come back.

Everything ok, when selecting it with the finger.

Selecting is fine

Bug when selecting programmatically -> image is gone

Per Code Step 1Per Code Step 2Per Code Step 3

Any suggestions what I'm doing wrong?

Best regards, Steve

like image 472
user707342 Avatar asked Oct 07 '12 10:10

user707342


People also ask

How to reload data from uitableviewcontroller?

Were you using a UITableViewController? One of the few things it does is call reloadData () on the table view. Now you have to do it yourself. Place it after you assign the data source to the table view.

Why use a UITableView instead of a Tableview?

Table views are more versatile than you might think. For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views.

Should I use a scroll view or a UITableView?

For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views. The code of the table view data source often ends inside view controllers when it should go into a separate class.

What are the cells of UITableView?

The cells of UITableView are instances of UITableViewCell or its subclasses. It is the table view that adds, removes, and arranges cells in its view hierarchy. Table views reuse cells that go out of the screen to display new elements, so that:


1 Answers

Selecting the More Navigation Controller

To select the moreNavigationController, you must set the selectedViewController:

self.tabBarController.selectedViewController = self.tabBarController.moreNavigationController;

The reason for that is simple: The moreNavigationController is not inside the viewControllers of the tab bar controller, so it can't be selected by index.

To select one of the "more" controllers directly, you can either set the selectedViewController property:

self.tabBarController.selectedViewController = viewController5;

Or you can set the selectedIndex:

self.tabBarController.selectedIndex = 5;

Fixing the Disappearing Tab Bar Image

The disappearing tab bar image is caused by using a navigation controller (for the settings) inside a navigation controller (the moreNavigationController generated by the tab bar controller). To fix it, there are two solutions:

  1. Don't add navigation controllers for the more section, but add the controllers directly. The controller structure would look like this, assuming the controllers in all tabs need navigation:

    tabBarController
    + navigationController
      + viewController0
    + navigationController
      + viewController1
    + navigationController
      + viewController2
    + navigationController
      + viewController3
    + viewController4
    + viewController5
    
  2. Set the tabBarItem of the settings controller on its navigation controller (you only need to do this once):

    UINavigationController *settingsNavigationController = [appDelegate.objTabBarController objectAtIndex:5];
    UIViewController *settingsRootController = settingsNavigationController.viewControllers[0];
    settingsNavigationController.tabBarItem = settingsRootController.tabBarItem;
    
like image 97
Tammo Freese Avatar answered Sep 23 '22 07:09

Tammo Freese