简体   繁体   中英

Change TabBar text color for selected item only (Xamarin.Forms iOS)

I'm writing an app that has a TabBar. On iOS, I want to change the selected items' highlight to be green instead of this default blue.

蓝色标签

I can use this line of code when creating the TabbedPage: BarTextColor = Color.FromHex("#27b286");

This changes the Icon color as I want it to, but it also changes the textcolor on ALL tabs, not just the selected one (I want the selected tab text to be green).

绿色标签

The TabPage code is:

 NavigationPage.SetHasNavigationBar(this, false);

        if(Device.RuntimePlatform == "iOS")
        {
            BarBackgroundColor = Color.White;
            //BarTextColor = Color.FromHex("#27b286");

            Children.Add(new CoinsPage() { Title = "Coins", Icon = "coins.png" });
            Children.Add(new PortfolioPage() { Title = "Portfolio", Icon = "portfolio.png" });
            Children.Add(new TrendingPage() { Title = "Trending", Icon = "trending.png" });
            Children.Add(new SettingsPage() { Title = "Settings", Icon = "settings.png" });
        }

How can I make ONLY the selected tab text color green?

Not sure if this is intended to work like that. I think it would make sense because we are setting the text color and not the selected color or anything like that.

So, it seems there is no way to do this from pure Forms right now.

To set it for iOS, go into the AppDelegate.cs file and in your FinishedLaunching method add this line:

UITabBar.Appearance.TintColor = UIColor.Red;

Of course, replacing the color with your own. It should now just be the selected item that is colored.

UITabBar.Appearance.SelectedImageTintColor = UIColor.FromRGB(0, 122, 255);

        // Color of the tabbar background:
        UITabBar.Appearance.BarTintColor = UIColor.FromRGB(247, 247, 247);

        // Color of the selected tab text color:
        UITabBarItem.Appearance.SetTitleTextAttributes(
            new UITextAttributes()
            {
                TextColor = UIColor.FromRGB(0, 122, 255)
            },
            UIControlState.Selected);

        // Color of the unselected tab icon & text:
        UITabBarItem.Appearance.SetTitleTextAttributes(
            new UITextAttributes()
            {
                TextColor = UIColor.FromRGB(146, 146, 146)
            },
            UIControlState.Normal);

update this code in IOS file AppDelegate inside the method FinishedLaunching after LoadApplication(new App());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM