我开发了一个小型的Xamarin.Forms.Shell应用程序,我没有找到如何为StatusBar 前台和背景应用自定义颜色。
我的应用程序使用了一个非常基本的配色方案:
NavigationBar和TabBar的前台NavigationBar TabBar我希望为StatusBar保留相同的颜色,但事实并非如此:
StatusBar颜色似乎由iOS管理。=>在不管理DarkMode的设备上,或者当LightMode是active时,StatusBar信息显示得很好

=>,但当DarkMode是活动的时,情况并非如此,因为这些信息是隐藏的

StatusBar颜色似乎由styles.xaml文件和android:statusBarColor属性管理。=>如果我指定了白色颜色,则StatusBar信息是不可见的,因为也有白色。

=>而如果我指定灰色颜色,则StatusBar信息是可见的。

所以我试着应用一个给定那里的解决方案
StatusBar信息是不可见的,因为也有白色信息

=>如何管理iOS StatusBar前景色?对Android来说足够了吗?
发布于 2020-12-09 07:38:29
您可以对iOS (带有黑色文本的白色状态栏)使用以下方法
public void SetWhiteStatusBar()
{
Device.BeginInvokeOnMainThread(() =>
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
statusBar.BackgroundColor = 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.White;
}
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);
GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
});
}核对我的问题/答案
发布于 2020-12-04 03:24:03
通过在这条线.中的解决方案,操作系统解决了这个问题。
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下,状态栏文本的颜色将改为白色。如果您不支持黑暗模式,仍然支持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/>参考:更喜欢这种风格
https://stackoverflow.com/questions/65132070
复制相似问题