Xamarin.forms 表单-在没有导航栏的情况下更改状态栏的颜色

Xamarin.forms 表单-在没有导航栏的情况下更改状态栏的颜色,xamarin.forms,xamarin.ios,Xamarin.forms,Xamarin.ios,这显示了如何为iOS设置状态栏颜色。但是,我的页面上有HasNavigationBar=false,因此当您不使用导航栏时,如何设置颜色 我的页面 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" NavigationPage.HasNavigationBar="

这显示了如何为iOS设置状态栏颜色。但是,我的页面上有HasNavigationBar=false,因此当您不使用导航栏时,如何设置颜色

我的页面

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             NavigationPage.HasNavigationBar="false">

您可以在iOS项目中向AppDelegate类的FinishedLaunching方法添加代码。例如,将状态栏颜色设置为绿色

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
   // set status bar color to green
   UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
   statusBar.BackgroundColor = UIColor.FromRGB(61, 205, 88);

   // the usual suspects follow
   global::Xamarin.Forms.Forms.Init();
   LoadApplication(new App());

   return base.FinishedLaunching(app, options);
}

希望这有帮助。

此AppDelegate代码更改iOS 13和旧iOS版本的状态栏颜色

  public override bool FinishedLaunching(UIApplication app, NSDictionary options)
  {
        int red = 11;
        int green = 22;
        int blue = 33;

        // the usual Xamarin.Forms code
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        bool canLoadUrl = base.FinishedLaunching(app, options);

        // get status bar and set color
        UIView statusBar;
        if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
        {
            const int tag = 999; 

            var window = UIApplication.SharedApplication.Delegate.GetWindow();
            if (window is null) return null;

            statusBar = window.ViewWithTag(tag) ?? new UIView(UIApplication.SharedApplication.StatusBarFrame)
            {
                Tag = tag
            };

            window.AddSubview(statusBar);
        }
        else
        {
            statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
        }
        if (!(statusBar is null))
        {
            statusBar.BackgroundColor = UIColor.FromRGB(red, green, blue);
        }

        return canLoadUrl;
   }

NavigationBar
StatusBar
是不同的东西。您必须在特定于平台的代码中执行此操作。在XF 3.5之前,没有现成的方法可以做到这一点。可以帮助您获得一个尝试。对于那些现在正在研究这个的人来说,它现在在iOS13中不再工作,并且将崩溃。您是否使用最新版本的Xamarin编译过?不是最新版本。该应用程序当前位于Xamarin.Forms 3.6上。我将尝试升级到最新的XF版本,看看是否有相同的问题