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

.NET MAUI:仅使用ContentPage时如何更改底部导航栏颜色?

修改.NET MAUI中ContentPage底部返回导航栏颜色的方法

1. 平台特定配置(推荐)

ContentPage本身没有直接控制底部返回导航栏颜色的属性,可针对不同平台单独配置,无需复杂自定义:

Android

在Platforms/Android/Resources/values/styles.xml的MainTheme中添加底部导航栏颜色设置:

<style name="MainTheme" parent="MainTheme.Base">
    <!-- 保留原有属性 -->
    <item name="android:navigationBarColor">#FF2ECC71</item> <!-- 设置底部导航栏背景色 -->
    <item name="android:windowLightNavigationBar">false</item> <!-- 可选:控制导航栏图标深浅,深色背景设为false -->
</style>

iOS

在Platforms/iOS/AppDelegate.cs的FinishedLaunching方法中添加全局配置:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    // 其他初始化代码
    UINavigationBar.Appearance.BarTintColor = UIColor.FromRGB(46, 204, 113); // 底部返回栏背景色
    UINavigationBar.Appearance.TintColor = UIColor.White; // 返回按钮等图标颜色
    return base.FinishedLaunching(app, options);
}

若需针对单个页面设置,可在ContentPage后台代码中添加平台判断:

protected override void OnAppearing()
{
    base.OnAppearing();
#if IOS
    if (NavigationController != null)
    {
        NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB(52, 152, 219);
        NavigationController.NavigationBar.TintColor = UIColor.White;
    }
#endif
}

2. 自定义渲染器(精细控制场景)

如果需要更灵活的页面级控制,可自定义ContentPage渲染器:

Android渲染器

在Platforms/Android/Renderers/CustomContentPageRenderer.cs中编写:

using Android.Content;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls;

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomContentPageRenderer))]
namespace YourAppNamespace.Platforms.Android.Renderers
{
    public class CustomContentPageRenderer : PageRenderer
    {
        public CustomContentPageRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                Window?.SetNavigationBarColor(Android.Graphics.Color.Parse("#FF3498DB"));
            }
        }
    }
}

iOS渲染器

在Platforms/iOS/Renderers/CustomContentPageRenderer.cs中编写:

using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls;
using UIKit;

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomContentPageRenderer))]
namespace YourAppNamespace.Platforms.iOS.Renderers
{
    public class CustomContentPageRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
            if (NavigationController != null)
            {
                NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB(155, 89, 182);
                NavigationController.NavigationBar.TintColor = UIColor.White;
            }
        }
    }
}

3. 额外说明

  • 若使用.NET MAUI Shell,可在AppShell.xaml中通过Shell.NavBarBackgroundColor和Shell.NavBarTextColor全局设置导航栏样式(包含底部返回栏),但Shell配置会覆盖页面级自定义。
  • Android设备的导航栏颜色可能受系统主题影响,需确保应用主题未继承系统默认导航栏设置。

内容的提问来源于stack exchange,提问作者Cody

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 02:25:24