mauihandleruitabbarbottomnavigationviewtabbedpage

How can I make custom tabbed page in MAUI?


I created a tabbed page, and I am trying to achieve this kind of Bottom Tab Bar. enter image description here

I manage to do this in Android using TabbedViewHandler, but there is an unwanted white layer behind the custom tab bar, how can I remove this? and also I couldn't found a similar solution for iOS.

enter image description here

I tried this for Android:

using Microsoft.Maui.Handlers;
using Microsoft.Maui.Controls.Platform;
using System.Reflection;
#if ANDROID
using static Android.Views.ViewGroup;
using Android.Graphics.Drawables;
using Google.Android.Material.BottomNavigation;
using AndroidX.Core.View;
#endif

namespace Project.Views.Handlers
{
    public class AndroidTabbedPageHandler : TabbedViewHandler
    {
        public AndroidTabbedPageHandler()
        {
            Mapper.AppendToMapping(nameof(CustomTabbedPage), (handler, view) =>
            {
#if ANDROID
                if (view is CustomTabbedPage)
                {
                    FieldInfo tabbedPageManagerFieldInfo = typeof(TabbedPage).GetField("_tabbedPageManager", BindingFlags.NonPublic | BindingFlags.Instance);
                    object tabbedPageManager = tabbedPageManagerFieldInfo?.GetValue(view);

                    FieldInfo tabLayoutFieldInfo = tabbedPageManager?.GetType().GetField("_bottomNavigationView", BindingFlags.NonPublic | BindingFlags.Instance);
                    BottomNavigationView bottomNavigationView = tabLayoutFieldInfo?.GetValue(tabbedPageManager) as BottomNavigationView;

                    if (bottomNavigationView != null)
                    {
                        GradientDrawable drawable = new GradientDrawable();
                        drawable.SetShape(ShapeType.Rectangle);
                        drawable.SetColor(Android.Graphics.Color.White);
                        drawable.SetCornerRadius(40);
                        MarginLayoutParams layoutParams = new(LayoutParams.WrapContent, LayoutParams.WrapContent);
                        layoutParams.SetMargins(20, 0, 20, 40);
                        bottomNavigationView.LayoutParameters = layoutParams;
                        bottomNavigationView.LabelVisibilityMode = LabelVisibilityMode.LabelVisibilityUnlabeled;
                        bottomNavigationView.SetElevation(12);
                        ViewCompat.SetBackground(bottomNavigationView, drawable);
                    }
                }
#endif
            });
        }
    }
}

Solution

  • I had the same problem, on Android you can change the color of unwanted white layer behind the custom tab bar with this code:

    var activity = Platform.CurrentActivity;
    if (activity?.Window?.DecorView != null)
    {
        activity.Window.DecorView.SetBackgroundColor(Graphics.Color.WhiteSmoke);
    }
    
    Complete code:
    public class CustomTabbedPageHandler : TabbedViewHandler
    {
    
        public CustomTabbedPageHandler()
        {
            Mapper.AppendToMapping(nameof(TabbedPage), (handler, view) =>
            {
                if (view is TabbedPage)
                {
                    FieldInfo tabbedPageManagerFieldInfo = typeof(TabbedPage).GetField("_tabbedPageManager", BindingFlags.NonPublic | BindingFlags.Instance);
                    object tabbedPageManager = tabbedPageManagerFieldInfo?.GetValue(view);
    
                    FieldInfo tabLayoutFieldInfo = tabbedPageManager?.GetType().GetField("_bottomNavigationView", BindingFlags.NonPublic | BindingFlags.Instance);
                    BottomNavigationView bottomNavigationView = tabLayoutFieldInfo?.GetValue(tabbedPageManager) as BottomNavigationView;
    
                    if (bottomNavigationView != null)
                    {
                        GradientDrawable backgroundDrawable = new();
                        backgroundDrawable.SetColor(Graphics.Color.White);
                        backgroundDrawable.SetShape(ShapeType.Rectangle);
                        backgroundDrawable.SetCornerRadius(40);
    
                        ViewCompat.SetBackground(bottomNavigationView, backgroundDrawable);
    
                        var layoutParams = new ViewGroup.MarginLayoutParams(
                            ViewGroup.LayoutParams.MatchParent,
                            ViewGroup.LayoutParams.WrapContent
                        );
                        layoutParams.SetMargins(20, 20, 20, 20);
                        bottomNavigationView.LayoutParameters = layoutParams;
    
                        var activity = Platform.CurrentActivity;
                        if (activity?.Window?.DecorView != null)
                        {
                            activity.Window.DecorView.SetBackgroundColor(Graphics.Color.WhiteSmoke);
                        }
                    }
                }
            });
        }
    }