繁体   English   中英

Xamarin Forms iOS 状态栏文本颜色

[英]Xamarin Forms iOS status bar text color

我无法将 Xamarin Forms iOS 应用的状态栏文本颜色更改为白色。 我在 info.plist 中有如下更改:

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

然而颜色仍然是黑色..有没有另一种方法来改变状态栏文本的颜色?

在 Xamarin.Forms 中,您需要做三件事才能在 iOS 状态栏中实现白色文本。 我还在下面发布了一个示例 Xamarin.Forms 应用程序,该应用程序在 iOS 状态栏中使用白色文本。

1.更新Info.plist

Info.plist ,添加 Boolean Property View controller-based status bar appearance并将其值设置为No

在此处输入图像描述

2. 使用 NavigationPage 并将导航栏文本颜色设置为白色

Application类(通常是App.cs )中, MainPage必须是NavigationPage ,并且BarTextColor必须设置为Color.White 在此处输入图像描述

3.清理和重建应用程序

有时编译器在您清理并重建应用程序之前不会更新状态栏颜色,因此在步骤 1 和 2 中进行更改后,清理应用程序并重建它。 在此处输入图像描述

示例应用

https://github.com/brminnick/SaveImageToDatabaseSampleApp/

对我来说,在 IOS 中更改状态栏的唯一方法是在 AppDelegate 的 FinishedLaunching 中使用此代码

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init ();

    LoadApplication (.....);
    app.SetStatusBarStyle(UIStatusBarStyle.LightContent, true);

    return base.FinishedLaunching (app, options);
}

所以我为更改状态栏颜色状态栏文本颜色所做的是:

信息列表

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<!--<key>UIStatusBarHidden</key>-->
<!--<true/>-->

应用委托

在函数FinishedLaunching()中,添加以下代码:

UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar != null && statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
    statusBar.BackgroundColor = Color.FromHex("#7f6550").ToUIColor(); // change to your desired color 
}

应用程序.xaml

我在下面添加了代码以将状态栏文本颜色更改为White

<Style TargetType="{x:Type NavigationPage}">
     <!--<Setter Property="BarBackgroundColor" Value="Black" />-->
     <Setter Property="BarTextColor" Value="White" />
</Style> 

选择NavigationBar并将Style属性更改为Black

在此处输入图像描述

就我而言,将其添加到AppDelegate FinishedLaunching有效:

UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM