【问题标题】:Xamarin Forms TabbedPage Tab Unselected ColorXamarin 表单 TabbedPage 选项卡未选择的颜色
【发布时间】:2019-09-27 12:13:15
【问题描述】:

我正在尝试更改 TabbedPage 中 未选择 图标的颜色。

我有一个自定义的 TabbedRenderer :

public class CustomTabbedPage : TabbedRenderer
{
    public override void ViewWillAppear(bool animated)
    {
        if (TabBar == null) return;
        if (TabBar.Items == null) return;

        var tabs = Element as TabbedPage;
        if (tabs != null)
        {
            for (int i = 0; i < TabBar.Items.Length; i++)
            {
                UpdateItem(TabBar.Items[i], tabs.Children[i].Icon);
            }
        }

        base.ViewWillAppear(animated);
    }

    private void UpdateItem(UITabBarItem item, string icon)
    {
        if (item == null)
            return;
        try
        {
            icon = icon.Replace(".png", " Filled.png");
            if (item == null) return;
            if (item.SelectedImage == null) return;
            if (item.SelectedImage.AccessibilityIdentifier == icon) return;
            item.SelectedImage = UIImage.FromBundle(icon);
            item.SelectedImage.AccessibilityIdentifier = icon;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to set selected icon: " + ex);
        }
    }
}

我已经设法更改了选定项的文本颜色和未选定项的文本颜色,但没有更改图标颜色。

谢谢!

【问题讨论】:

标签: c# xamarin xamarin.ios xamarin.forms


【解决方案1】:

未选中的图标颜色: 在最开始或您的 ViewWillAppear 事件中放置一行:

        TabBar.UnselectedItemTintColor = UIColor.Red; //red is for the yay effect :)

【讨论】:

  • 这是在最新的 Xamarin.Forms 包中添加的。当我发布问题时,我正在运行 2.3.3.193。谢谢!
【解决方案2】:
private void UpdateItem(UITabBarItem item, string icon)
{
    if (item == null)
         return;
    try
    {
         string newIcon = icon.Replace(".png", " Filled.png");
         if (item == null) return;
         if (item.SelectedImage == null) return;
         if (item.SelectedImage.AccessibilityIdentifier == icon) return;

         item.Image = UIImage.FromBundle(icon);
         item.Image = item.Image.ImageWithRenderingMode(UIKit.UIImageRenderingMode.AlwaysOriginal);

         item.SelectedImage = UIImage.FromBundle(newIcon);
         item.SelectedImage = item.SelectedImage.ImageWithRenderingMode(UIKit.UIImageRenderingMode.AlwaysOriginal);

         item.SelectedImage.AccessibilityIdentifier = icon;
     }
     catch (Exception ex)
     {
         Console.WriteLine("Unable to set selected icon: " + ex);
     }
}

【讨论】:

    【解决方案3】:

    Xamarin Forms 4+ 现在在 TabbedPage 对象上具有 SelectedTabColorUnselectedTabColor 属性,可以直接从共享代码轻松设置,这会影响文本和图标(但在选择时换出不同的图像当然仍然需要自定义渲染器)。

    【讨论】:

    • 我可以确认这是可行的。您希望 var 名称以 Bar 开头,因为所有其他设置(例如 BarTextColor)都以 Bar 开头。 (对于智能感知很有用。)
    猜你喜欢
    • 2022-01-08
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多