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

.NET MAUI中如何用平台特定代码设置iOS状态栏颜色?

.NET MAUI iOS状态栏颜色设置(UIKit原生方案)

一、全局统一设置(所有页面生效)

1. 修改Info.plist

在Platforms/iOS/Info.plist中添加以下配置,开启控制器级别的状态栏样式控制:

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

2. 在SceneDelegate中添加代码(iOS 13+)

修改Platforms/iOS/SceneDelegate.cs的WillConnect方法:

public override void WillConnect(UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
    if (scene is UIWindowScene windowScene)
    {
        UIWindow window = new UIWindow(windowScene);
        Window = window;

        // 设置状态栏文字颜色:LightContent为白色,Default为黑色
        UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);

        // 创建状态栏背景视图并设置颜色
        var statusBarView = new UIView(UIApplication.SharedApplication.StatusBarFrame);
        statusBarView.BackgroundColor = UIColor.FromRGB(255, 0, 0); // 替换为你的目标颜色
        window.AddSubview(statusBarView);

        LoadApplication(new App());
        window.MakeKeyAndVisible();
    }

    base.WillConnect(scene, session, connectionOptions);
}

若适配iOS 12及以下,修改Platforms/iOS/AppDelegate.cs的FinishedLaunching方法,逻辑与上述一致。

二、页面独立设置(不同页面不同颜色)

创建iOS平台专属的页面渲染器,实现单个页面的状态栏颜色定制:

  1. 在Platforms/iOS目录下新建CustomPageRenderer.cs:
using Microsoft.Maui.Controls.Platform.iOS;
using UIKit;

[assembly: Microsoft.Maui.Controls.ExportRenderer(typeof(Microsoft.Maui.Controls.ContentPage), typeof(YourAppNamespace.Platforms.iOS.CustomPageRenderer))]
namespace YourAppNamespace.Platforms.iOS
{
    public class CustomPageRenderer : PageRenderer
    {
        private UIView? _statusBarBgView;

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            // 当前页面的状态栏文字颜色
            UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);

            // 创建背景视图并设置颜色
            _statusBarBgView = new UIView(UIApplication.SharedApplication.StatusBarFrame);
            _statusBarBgView.BackgroundColor = UIColor.FromRGB(0, 0, 255); // 当前页面的状态栏颜色
            View.AddSubview(_statusBarBgView);
        }

        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);
            // 页面消失时移除视图,避免残留
            _statusBarBgView?.RemoveFromSuperview();
            _statusBarBgView = null;
        }

        public override void ViewDidLayoutSubviews()
        {
            base.ViewDidLayoutSubviews();
            // 设备旋转时更新视图 frame,解决布局错乱
            if (_statusBarBgView != null)
            {
                _statusBarBgView.Frame = UIApplication.SharedApplication.StatusBarFrame;
            }
        }
    }
}
  1. 若需给特定页面单独设置,只需把ExportRenderer中的ContentPage替换为你的自定义页面类型即可。

方案说明

  • 直接使用UIKit原生API,避开Community Toolkit的Android崩溃问题;
  • 通过ViewDidLayoutSubviews监听布局变化,解决旋转时的状态栏背景错位问题;
  • 启动时在窗口初始化阶段添加状态栏视图,解决启动时不生效的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 23:45:34