Xamarin.Forms-更改没有导航栏的StatusBar颜色

时间:2019-09-16 22:11:59

标签: xamarin.forms xamarin.ios

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

我的页面...

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

2 个答案:

答案 0 :(得分:1)

您可以将代码添加到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);
}

希望这会有所帮助。

答案 1 :(得分:0)

此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;
   }