如何在 MAUI 中制作自定义选项卡式页面?

How can I make custom tabbed page in MAUI?

提问人:UmairAhmadK 提问时间:11/13/2023 最后编辑:UmairAhmadK 更新时间:11/17/2023 访问量:107

问:

我创建了一个选项卡式页面,我正在尝试实现这种底部选项卡栏。enter image description here

我设法使用 TabbedViewHandlerAndroid 中执行此操作,但是自定义标签栏后面有一个不需要的白色层,我该如何删除它?而且我找不到适用于 iOS 的类似解决方案

enter image description here

我在 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
            });
        }
    }
}
毛伊 岛处理程序 UITabbar BottomNavigationView TabbedPage

评论

0赞 Sean He-MSFT 11/14/2023
继承的 TabbedViewHandler 是否与 TabbedViewHandle 相同?创建自定义处理程序需要属性映射器,您能否提供相关代码以便我进行测试?
0赞 FreakyAli 11/14/2023
试试这个,我想 github.com/roubachof/Sharpnado.Tabs

答:

1赞 Sean He-MSFT 11/17/2023 #1

对于 iOS,请参考 .NET MAUI 中的 TabbarRenderer 源码,TabbedPage 是在 iOS 平台上实现的,所以可以尝试新建一个 Renderer 类并让它继承。TabbedRendererTabbedRenderer

具体来说,您可以通过覆盖 iOS 本机方法来更改 Tabbar 的位置和角度,其名称如下代码所示。ViewDidLayoutSubviews

    public class CustomTabbar : TabbedRenderer
    {
#if iOS
        public override void ViewDidLayoutSubviews()
        {
            Tabbar.Layer.CornerRadius = 40;
            Tabbar.Layer.MasksToBounds = True;
            TabBar.Frame = new CoreGraphics.CGRect(TabBar.Frame.X + 10, TabBar.Frame.Y - 20, TabBar.Frame.Width - 20, TabBar.Frame.Height + 5);
        }
#end if
    }