【问题标题】:Xamarin Shell tabbar color changes on iOS when there is content behind it当 iOS 后面有内容时,Xamarin Shell 选项卡栏颜色会发生变化
【发布时间】:2022-01-27 09:57:14
【问题描述】:

我有一个带有自定义渲染器的 Shell Tabbar 和一个位于它后面的 Collection 视图。 现在,当我的 Collection 视图中有内容位于标签栏后面时,标签栏背景颜色变白。

之前 enter image description hereenter image description here

我尝试关闭我的自定义渲染器,它按预期工作并且没有颜色变化,所以我知道它在我的自定义渲染器中,但我不知道它是什么。

自定义渲染器:

public class MyShellRenderer : ShellRenderer
{
    protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
    {
        var renderer = base.CreateShellSectionRenderer(shellSection);
        if (renderer != null)
        {
        }
        return renderer;
    }
    protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
    {
        return new CustomTabbarAppearance();
    }

}
public class CustomTabbarAppearance : IShellTabBarAppearanceTracker
{
    public void Dispose()
    {
    }

    public void ResetAppearance(UITabBarController controller)
    {
    }

    public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
    {
        UITabBar myTabBar = controller.TabBar;
        UIColor tabBarColor = ((Xamarin.Forms.Color)Xamarin.Forms.Application.Current.Resources["MyTabBarColor"]).ToUIColor();

        if (myTabBar != null)
        {
            UIView view = new UIView(new CGRect(0, 0, myTabBar.Frame.Width, 2)) { BackgroundColor = Color.FromRgb(17, 17, 17).ToUIColor() };
            myTabBar.AddSubview(view);

            myTabBar.BackgroundColor = tabBarColor;//change
            myTabBar.BarTintColor = tabBarColor;

            myTabBar.UnselectedItemTintColor = UIColor.White;

            if (myTabBar.Items != null)
            {
                foreach (UITabBarItem item in myTabBar.Items)
                {
                    item.Title = null;
                    item.ImageInsets = new UIEdgeInsets(10, 0, 0, 0);
                }
            }
        }
    }

    public void UpdateLayout(UITabBarController controller)
    {

    }

}

【问题讨论】:

  • 听起来标签栏背景颜色有一些透明度。直接在自定义渲染器中使用硬编码的背景颜色进行测试,看看是否可以让症状停止。
  • 我已经在自定义渲染器中直接设置了背景颜色。但注意到当我移除 bartintcolor 时,它会变得更亮,所以我认为这与此有关。
  • 这两者之间似乎有一些互动。谷歌xamarin ios bartintcolor vs background color。很多讨论 - 我不知道其中任何一个是否“解决”它。

标签: ios xamarin xamarin.forms xamarin.ios


【解决方案1】:

尝试在您的代码中硬编码tabBarColor,并将Xamarin.Forms 更新到最新版本。


更重要的是,对你的代码有一个小建议。

SetAppearance方法在切换Tabbar时调用,所以一直在添加视图。

应该只添加一次视图,最好改变逻辑。

更新

UIView myView = null;
public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
{
    UITabBar myTabBar = controller.TabBar;
    UIColor tabBarColor = UIColor.White;  //to any color you want

    if (myTabBar != null)
    {
       if (myView != null) return;

       myView = new UIView(new CGRect(0, 0, myTabBar.Frame.Width, 2)) { BackgroundColor = Color.FromRgb(17, 17, 17).ToUIColor() };
               
       myTabBar.AddSubview(myView);

       myTabBar.BackgroundColor = tabBarColor;//change
       myTabBar.BarTintColor = tabBarColor;

       myTabBar.UnselectedItemTintColor = UIColor.White;

       if (myTabBar.Items != null)
       {
           foreach (UITabBarItem item in myTabBar.Items)
           {
               item.Title = null;
               item.ImageInsets = new UIEdgeInsets(10, 0, 0, 0);
           }
        }
    }
}

【讨论】:

  • 嘿,谢谢,你能提供我硬编码颜色的代码吗?你有任何关于如何改变我的逻辑的提示吗?
  • 检查我的更新。
  • 遗憾的是,一旦我将颜色从白色更改为黑色,问题仍然存在。
【解决方案2】:

我现在解决了,带有透明度的提示是正确的,只是不是颜色的透明度,我的标签栏不知何故设置为半透明

我的工作代码:

UIView myView = null;
    public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
    {
        UITabBar myTabBar = controller.TabBar;
        UIColor tabBarColor = ((Xamarin.Forms.Color)Xamarin.Forms.Application.Current.Resources["MyTabBarColor"]).ToUIColor();

        if (myTabBar != null)
        {
            myView = new UIView(new CGRect(0, 0, myTabBar.Frame.Width, 2)) { BackgroundColor = Color.FromRgb(17, 17, 17).ToUIColor() };
            myTabBar.AddSubview(myView);

            myTabBar.BackgroundColor = tabBarColor;
            myTabBar.BarTintColor = tabBarColor;
            myTabBar.Translucent = false;

            myTabBar.UnselectedItemTintColor = UIColor.White;

            if (myTabBar.Items != null)
            {
                foreach (UITabBarItem item in myTabBar.Items)
                {
                    item.Title = null;
                    item.ImageInsets = new UIEdgeInsets(10, 0, 0, 0);
                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 2019-03-11
    • 2014-09-04
    • 2017-01-24
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多