Op通过this thread中的解决方案解决了这个问题。
public void SetColoredStatusBar(string hexColor)
{
Device.BeginInvokeOnMainThread(() =>
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
statusBar.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
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 = Color.FromHex(hexColor).ToUIColor();
}
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}
==================================================== ========================
你的应用程序支持dark mode
吗?
如果您的应用程序在 下DarkMode
,状态栏文本颜色将变为白色。如果您不支持 Darkmode 并且仍然white for the Background of the NavigationBar and the TabBar
,则白色文本在白色背景下将不可见。
您可以使用AppThemeBinding在不同的模式下设置不同的颜色。
<Shell.Resources>
<ResourceDictionary>
<Color x:Key="NavigationPrimary">#2196F3</Color>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{AppThemeBinding Light=White, Dark=Black}" />
<Setter Property="Shell.ForegroundColor" Value="{AppThemeBinding Light=Black, Dark=White}" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
<Setter Property="Shell.TabBarBackgroundColor" Value="White" />
<Setter Property="Shell.TabBarForegroundColor" Value="Red"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="Black"/>
<Setter Property="Shell.TabBarTitleColor" Value="Black"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
如果您想在不同的应用程序主题下保持状态栏颜色相同,请使用自定义渲染器:
[assembly:ExportRenderer (typeof(ContentPage), typeof(customPageRenderer))]
namespace App479.iOS
{
public class customPageRenderer : PageRenderer
{
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
this.NavigationController.NavigationBar.BarStyle = UIBarStyle.Default;
}
//if you content page does not have a NavigationBar, oveerride this method
public override UIStatusBarStyle PreferredStatusBarStyle()
{
return UIStatusBarStyle.DarkContent;
}
}
}
并在 info.plist 中添加一个键:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
参考:preferredstatusbarstyle