【问题标题】:Xamarin Forms iOS status bar text colorXamarin Forms iOS 状态栏文本颜色
【发布时间】:2017-10-29 21:56:23
【问题描述】:

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

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

但颜色仍然是黑色.. 有没有其他方法可以更改状态栏文本颜色?

【问题讨论】:

    标签: ios xamarin xamarin.ios xamarin.forms


    【解决方案1】:

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

    1。更新 Info.plist

    Info.plist中,添加布尔属性View controller-based status bar appearance并将其值设置为No

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

    Application 类(通常为App.cs)中,MainPage 必须是NavigationPageBarTextColor 必须设置为Color.White

    3。清理和重建应用程序

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

    示例应用

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

    【讨论】:

    • 感谢您的回答!但是现在我没有状态栏了:/
    • 哦不!您介意在 StackOverflow 上提出一个新问题并将链接发送给我吗?我也可以发布一些屏幕截图来帮助您调试该问题
    • 进入 Info.plist 的实际值(例如对于 windows 用户):UIViewControllerBasedStatusBarAppearance
    • 我不得不直接更改 info.plist。编辑器中的更改似乎没有影响文件。这可能是 Visual Studio for Mac 中的一个错误。
    【解决方案2】:

    对我来说,在 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);
    }
    

    【讨论】:

    • @Singapore 您必须在 info.plist 中将“基于控制器的状态栏外观”更改为 false(如接受的答案所示)。
    • 我不得不直接更改 info.plist。编辑器中的更改似乎没有影响文件。这可能是 Visual Studio for Mac 中的一个错误。
    【解决方案3】:

    所以我更改状态栏颜色状态栏文本颜色的方法是:

    Info.plist

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

    AppDelegate

    函数内部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 
    }
    

    App.xaml

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

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

    【讨论】:

    • 注意:UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;自iOS 13(例如iPhone 11)起导致异常,请在使用前检查操作系统版本。 UIDevice.CurrentDevice.CheckSystemVersion(13, 0)
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 1970-01-01
      • 2019-06-15
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多