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

Xamarin.Forms使用AppShell时如何在ContentPage中修改状态栏颜色

异常原因

你遇到的报错是因为iOS 13及以上版本废弃了UIApplication的statusBar相关属性,必须通过窗口场景的statusBarManager操作,旧方案的API已经不兼容新系统版本。

实现方案

我们可以通过依赖服务封装双端逻辑,实现和Android端一致的单行调用体验:

1. 共享项目定义跨平台接口

public interface IStatusBarService
{
    void SetStatusBarColor(Color color);
}

2. 各平台实现接口

iOS端实现(iOS项目中添加)
[assembly: Dependency(typeof(StatusBarService))]
namespace 你的应用命名空间.iOS
{
    public class StatusBarService : IStatusBarService
    {
        public void SetStatusBarColor(Color color)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                var keyWindow = UIApplication.SharedApplication.Windows.FirstOrDefault(w => w.IsKeyWindow);
                if (keyWindow?.WindowScene?.StatusBarManager == null) return;

                var statusBarFrame = keyWindow.WindowScene.StatusBarManager.StatusBarFrame;
                var statusBarView = new UIView(statusBarFrame)
                {
                    BackgroundColor = color.ToUIColor()
                };
                keyWindow.AddSubview(statusBarView);
            }
            else
            {
                // 兼容iOS 13以下版本
                var statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
                if (statusBar != null && statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
                {
                    statusBar.BackgroundColor = color.ToUIColor();
                }
            }
            // 可根据背景色深浅调整文字样式,这里示例用白色文字
            UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
        }
    }
}
Android端实现(Android项目中添加,统一调用逻辑)
[assembly: Dependency(typeof(StatusBarService))]
namespace 你的应用命名空间.Droid
{
    public class StatusBarService : IStatusBarService
    {
        public void SetStatusBarColor(Color color)
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                var activity = Xamarin.Essentials.Platform.CurrentActivity;
                activity.Window.SetStatusBarColor(color.ToAndroid());
                // 自动适配状态栏文字颜色
                var luminance = color.R * 0.299 + color.G * 0.587 + color.B * 0.114;
                activity.Window.DecorView.SystemUiVisibility = luminance < 0.5 
                    ? (StatusBarVisibility)SystemUiFlags.LightStatusBar 
                    : 0;
            }
        }
    }
}

3. ContentPage中调用

在需要设置状态栏颜色的页面OnAppearing生命周期中加一行代码即可:

protected override void OnAppearing()
{
    base.OnAppearing();
    DependencyService.Get<IStatusBarService>().SetStatusBarColor(Color.FromRgb(33, 150, 243));
}

注意事项

iOS端需要在Info.plist中添加配置,把UIViewControllerBasedStatusBarAppearance设为false,否则状态栏样式修改不生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 20:27:03