Xamarin iOS tab bar - Selecting an item in the More menu

EvidentSolutions_Craig 0 Reputation points
2023-10-16T23:41:37.3866667+00:00

Hi,

I'm having an issue with a Xamarin iOS app and the UITabBar in a TabBarController, specifically with selecting a tab bar item that is in the More section.

I have 10 tab bar items in the tab bar (including the More button). When the app starts up, I cycle through and remove one or more items depending upon the user's permissions. I use the Title of the tab bar item to find it and remove it, thus:

private void RemoveTab(string title)
{
    if (TabBar.Items != null)
    {
        System.Diagnostics.Debug.WriteLine(string.Format("****** removing tab title = {0}, total tabs = {1}", title, TabBar.Items.Count()));

        for (int i = 0; i < TabBar.Items.Length; i++)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("****** title = {0}, title in toolbar is {1}", title, TabBar.Items[i].Title));

            if (TabBar.Items[i].Title == title)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("****** FOUND {0}", title, TabBar.Items[i].Title));

                List<UIViewController> viewControllers = ViewControllers.ToList();

                System.Diagnostics.Debug.WriteLine(string.Format("****** before remove total tabs = {0}", TabBar.Items.Count()));
                System.Diagnostics.Debug.WriteLine(string.Format("****** before remove list count = {0}", viewControllers.Count()));

                viewControllers.RemoveAt(i);

                System.Diagnostics.Debug.WriteLine(string.Format("****** after remove total tabs = {0}", TabBar.Items.Count()));
                System.Diagnostics.Debug.WriteLine(string.Format("****** after remove list count = {0}", viewControllers.Count()));
                System.Diagnostics.Debug.WriteLine(string.Format("****** after remove array count = {0}", viewControllers.ToArray().Count()));

                ViewControllers = viewControllers.ToArray();

                System.Diagnostics.Debug.WriteLine(string.Format("****** after assignment array count = {0}", ViewControllers.Count()));
                System.Diagnostics.Debug.WriteLine(string.Format("****** after assignment title = {0}, total tabs = {1}", title, TabBar.Items.Count()));

                break;
            }
        }
    }
}

The output oof the above method is as follows:

[0:] ****** removing tab title = Routes, total tabs = 10 [0:] ****** title = Routes, title in toolbar is Quick Quotes [0:] ****** title = Routes, title in toolbar is Charters [0:] ****** title = Routes, title in toolbar is Parent [0:] ****** title = Routes, title in toolbar is Routes [0:] ****** FOUND Routes [0:] ****** before remove total tabs = 10 [0:] ****** before remove list count = 10 [0:] ****** after remove total tabs = 10 [0:] ****** after remove list count = 9 [0:] ****** after remove array count = 9 [0:] ****** after assignment array count = 9 [0:] ****** after assignment title = Routes, total tabs = 5

As you can see, the total number of tabs prior to removing a tab is 10, however, after assigning of the updated ViewControllers, the number of tabs drops to 5 (I assume those visible, including More).

Further attempts to iterate through the tabs and find a tab by Title that is in the More section results in it not being found.

So, I'm wondering why the number of tabs suddenly drops to 5 after removing a ViewController and reassigning.

FWIW, I can also override ViewWillAppear(), print the number of tabs which is 10, and call base.ViewWillAppear(), then print the number of tabs again, and it is 5:

Any thoughts?

Developer technologies | .NET | Xamarin
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-10-17T06:42:38.5633333+00:00

    Hello,

    total tabs = 5

    This is in line with expectations.

    In iOS, if there are more than 5 viewControllers, the extra TabItems are encapsulated in the More tab. When you click on the More tab, a table view will pop up to show the rest of the TabItems.

    Therefore, when you call TabBar.Items.Count(), the value returned is 5.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.