当后面有内容时,Xamarin Shell iOS 上的标签栏颜色会发生变化

Xamarin Shell tabbar color changes on iOS when there is content behind it

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

之前 enter image description here 后 enter 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)
    {

    }

}

尝试在您的代码中硬编码 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);
           }
        }
    }
}

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

我的工作代码:

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