繁体   English   中英

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

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

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

蓝色标签

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

这会根据需要更改图标的颜色,但是也会更改所有选项卡上的文本颜色,而不仅仅是所选的颜色(我希望所选的选项卡文本为绿色)。

绿色标签

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

如何仅使选定的标签文本变为绿色?

不知道这是否打算那样工作。 我认为这是有道理的,因为我们正在设置文本颜色,而不是选定的颜色或类似的颜色。

因此,目前看来尚无从纯Forms进行此操作的方法。

要将其设置为iOS,请进入AppDelegate.cs文件,并在FinishedLaunching方法中添加以下行:

UITabBar.Appearance.TintColor = UIColor.Red;

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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