仅更改所选项目的TabBar文本颜色(Xamarin.Forms iOS)

时间:2018-09-06 09:03:28

标签: c# ios xaml xamarin.forms

我正在编写一个具有TabBar的应用程序。在iOS上,我想将所选项目的突出显示更改为绿色,而不是默认的蓝色。

Blue Tabs

创建TabbedPage时可以使用以下代码行: BarTextColor = Color.FromHex("#27b286");

这会更改图标的颜色,但同时也会更改所有标签上的文本颜色,而不仅仅是选定的标签(我希望选定的标签文本为绿色)。

Green Tabs

TabPage代码为:

 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" });
        }

如何仅将所选标签文本设置为绿色?

2 个答案:

答案 0 :(得分:5)

不确定是否打算那样工作。我认为这是有道理的,因为我们设置的是 text 颜色,而不是所选的颜色或类似的颜色。

因此,看来目前无法通过纯Forms进行此操作。

要在iOS上进行设置,请进入AppDelegate.cs文件,然后在您的FinishedLaunching方法中添加以下行:

UITabBar.Appearance.TintColor = UIColor.Red;

当然,可以用您自己的颜色替换。现在它应该只是彩色的所选项目。

答案 1 :(得分:2)

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);

在完成后的方法FinishedLaunching中的IOS文件AppDelegate中更新此代码  LoadApplication(new App());