MAUI中iOS标题栏边框无法正常隐藏及标题颜色修改问题
MAUI iOS 标题栏边框隐藏与标题颜色修改方案
一、Renderer 是否为正确方案?
Renderer 是 Xamarin.Forms 时期的原生自定义方案,在 MAUI 中虽能兼容使用,但官方更推荐采用 Handler 实现原生控件自定义——Handler 是 MAUI 专为跨平台原生交互设计的架构,更轻量、贴合 MAUI 设计逻辑,维护成本更低。
二、推荐实现方式:使用 Handler 自定义导航栏
1. 全局配置(应用所有页面生效)
在 MauiProgram.cs 中注册自定义 Handler:
builder.ConfigureMauiHandlers(handlers => { #if IOS handlers.AddHandler(typeof(NavigationPage), typeof(CustomNavigationHandler)); #endif });
创建自定义 Handler 类:
using Microsoft.Maui.Handlers; using UIKit; namespace YourAppNamespace; public class CustomNavigationHandler : NavigationPageHandler { protected override void ConnectHandler(UINavigationController platformView) { base.ConnectHandler(platformView); // 隐藏导航栏边框 platformView.NavigationBar.ShadowImage = new UIImage(); platformView.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default); // 修改标题文本颜色 platformView.NavigationBar.TitleTextAttributes = new UIStringAttributes { ForegroundColor = UIColor.FromRGB(255, 0, 0) // 替换为目标颜色 }; // iOS 18 适配:处理滚动时的导航栏样式 if (UIDevice.CurrentDevice.CheckSystemVersion(18, 0)) { platformView.NavigationBar.ScrollEdgeAppearance = platformView.NavigationBar.StandardAppearance.Copy(); platformView.NavigationBar.ScrollEdgeAppearance.ShadowColor = UIColor.Clear; platformView.NavigationBar.ScrollEdgeAppearance.TitleTextAttributes = platformView.NavigationBar.TitleTextAttributes; } } }
2. 单页面配置(仅特定页面生效)
若只需给单个页面设置,可在页面的 OnAppearing 方法中直接修改:
protected override void OnAppearing() { base.OnAppearing(); #if IOS var navController = (UINavigationController)Application.Current.Windows.First().Handler.PlatformView; // 隐藏边框 navController.NavigationBar.ShadowImage = new UIImage(); navController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default); // 设置标题颜色 navController.NavigationBar.TitleTextAttributes = new UIStringAttributes { ForegroundColor = UIColor.Blue }; // iOS 18 滚动样式适配 if (UIDevice.CurrentDevice.CheckSystemVersion(18, 0)) { navController.NavigationBar.ScrollEdgeAppearance = navController.NavigationBar.StandardAppearance.Copy(); navController.NavigationBar.ScrollEdgeAppearance.ShadowColor = UIColor.Clear; navController.NavigationBar.ScrollEdgeAppearance.TitleTextAttributes = navController.NavigationBar.TitleTextAttributes; } #endif }
三、Renderer 方案的修复(若坚持使用)
标题颜色不生效多因 iOS 18 对导航栏 Appearance 优先级调整,需同时设置 StandardAppearance 和 ScrollEdgeAppearance:
using Xamarin.Forms.Platform.iOS; using UIKit; [assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))] namespace YourAppNamespace.iOS; public class CustomNavigationRenderer : NavigationRenderer { public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); // 隐藏边框 NavigationController.NavigationBar.ShadowImage = new UIImage(); NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default); // 设置标题颜色并兼容 iOS 18 var textAttributes = new UIStringAttributes { ForegroundColor = UIColor.Green }; NavigationController.NavigationBar.TitleTextAttributes = textAttributes; if (UIDevice.CurrentDevice.CheckSystemVersion(18, 0)) { NavigationController.NavigationBar.StandardAppearance.TitleTextAttributes = textAttributes; NavigationController.NavigationBar.ScrollEdgeAppearance = NavigationController.NavigationBar.StandardAppearance.Copy(); NavigationController.NavigationBar.ScrollEdgeAppearance.ShadowColor = UIColor.Clear; } } }
四、注意事项
- iOS 18 要求显式设置
ScrollEdgeAppearance,否则滚动时导航栏样式会恢复默认 - 检查
Info.plist中是否存在冲突的导航栏配置 - 若使用 Shell,需改为自定义
ShellRenderer或ShellSectionRenderer实现对应样式
内容的提问来源于stack exchange,提问作者Surendhar
相关产品推荐
相关产品推荐

