You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI iOS平台下如何增大Shell TabBar菜单项字体大小?

在.NET MAUI Shell TabBar中仅为iOS平台增大菜单项字体大小

你可以通过自定义iOS平台的Shell Handler实现需求,这种方式仅对iOS生效,不会影响其他平台。以下是具体实现步骤:

步骤1:注册自定义Shell Handler

在iOS项目的Program.cs中,注册自定义Shell Handler:

using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using YourAppName.Platforms.iOS.Handlers; // 替换为你的应用命名空间

namespace YourAppName;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

        // 仅针对iOS注册自定义Handler
#if IOS
        builder.ConfigureMauiHandlers(handlers =>
        {
            handlers.AddHandler<Shell, CustomShellHandler>();
        });
#endif

        return builder.Build();
    }
}

步骤2:实现自定义Shell Handler

在iOS项目中创建Handlers文件夹,添加CustomShellHandler.cs文件:

using Microsoft.Maui.Handlers;
using UIKit;

namespace YourAppName.Platforms.iOS.Handlers; // 替换为你的应用命名空间

public class CustomShellHandler : ShellHandler
{
    protected override void ConnectHandler(UIViewController platformView)
    {
        base.ConnectHandler(platformView);

        // 获取TabBar控制器并修改字体大小
        if (platformView is UITabBarController tabBarController)
        {
            // 设置选中状态的字体大小(可自行调整数值)
            var selectedTextAttributes = new UITextAttributes
            {
                Font = UIFont.SystemFontOfSize(16)
            };

            // 设置未选中状态的字体大小(可自行调整数值)
            var unselectedTextAttributes = new UITextAttributes
            {
                Font = UIFont.SystemFontOfSize(14)
            };

            tabBarController.TabBar.ItemSelectedTextAttributes = selectedTextAttributes;
            tabBarController.TabBar.ItemNormalTextAttributes = unselectedTextAttributes;
        }
    }
}

额外说明

  • 可根据需求调整16和14这两个字体大小数值;
  • 若需要粗体字体,可将UIFont.SystemFontOfSize替换为UIFont.BoldSystemFontOfSize;
  • 代码中的YourAppName需替换为你实际的应用命名空间;
  • 借助#if IOS条件编译,只有iOS平台会加载该自定义Handler,其他平台不受影响。

内容的提问来源于stack exchange,提问作者Alpay Çalışır

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 01:27:28