【问题标题】:Xamarin.Forms - Change StatusBar Color with no navigation barXamarin.Forms - 在没有导航栏的情况下更改状态栏颜色
【发布时间】:2020-01-17 18:32:18
【问题描述】:

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

我的页面...

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

【问题讨论】:

  • NavigationBarStatusBar 是不同的东西。您必须在特定于平台的代码中执行此操作。在 XF 3.5 之前,没有办法开箱即用地做到这一点。 This article可以帮你试一试

标签: xamarin.forms xamarin.ios


【解决方案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);
}

希望这会有所帮助。

【讨论】:

  • 对于那些现在看这个的人来说,这现在在 iOS13 中不再有效并且会崩溃。
  • 您是否使用最新版本的 Xamarin 进行编译?
  • 不是最新的。该应用程序当前在 Xamarin.Forms 3.6 上。我会尝试升级到最新的 XF 版本,看看是否有同样的问题
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 2018-07-04
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    相关资源
    最近更新 更多