【问题标题】:Xamarion.iOS statusBar BackgroundColor won‘t changeXamarin.iOS statusBar BackgroundColor 不会改变
【发布时间】:2019-11-13 22:12:49
【问题描述】:

由于 iOS13 的暗模式,我正在努力在我的 Xamarin.Forms 应用程序中支持它。显示 NavigationBar 时,除了 StatusBar 颜色之外,一切正常。 Bar 的通常行为应该是根据导航栏的 BGColor 更改背景颜色,但由于某种原因它始终保持白色。 我试过像这样手动上色

UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView; statusBar.BackgroundColor = UIColor.FromRGB(61, 205, 88);

但它并没有改变任何东西。 下面是它的外观截图

【问题讨论】:

  • 如果回答对您有帮助,请记得稍后有时间标记ot投票。提前谢谢:)

标签: ios xamarin.forms xamarin.ios ios13


【解决方案1】:

在 Xamarin.Forms 应用程序中,修改状态栏需要在 iOS 解决方案中编辑Info.plist

首先,将UIViewControllerBasedStatusBarAppearance添加到文件中:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
//Follow can change text color of status bar
//<key>UIStatusBarStyle</key>
//<string>UIStatusBarStyleDarkContent</string>

第二,在Xamarin.iOS项目中,可以修改AppDelegate.cs中的statsbar背景,如下:

public override void OnActivated(UIApplication uiApplication)
{
    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
    {
        // If VS has updated to the latest version , you can use StatusBarManager , else use the first line code
        // UIView statusBar = new UIView(UIApplication.SharedApplication.StatusBarFrame);
        UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
        statusBar.BackgroundColor = UIColor.Red;
        statusBar.TintColor = UIColor.White;
        UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
    }
    else
    {
        UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
        if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
        {
            statusBar.BackgroundColor = UIColor.Red;
            statusBar.TintColor = UIColor.White;
            UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
        }
    }
    base.OnActivated(uiApplication);
}

你也可以看看this discussion

【讨论】:

  • 谢谢!这就像一个魅力,我不知道 iOS 13 及更高版本有不同的代码。顺便说一句,我在我的客户渲染器中使用TraitCollectionDidChange 方法执行此操作,也可以!
猜你喜欢
  • 2013-02-11
  • 2019-12-13
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2017-01-28
  • 2013-04-16
  • 1970-01-01
  • 2021-01-17
相关资源
最近更新 更多