0

我开发了一个小型Xamarin.Forms.Shell 应用程序,但没有找到如何为ForegroundBackground应用自定义颜色StatusBar

我的应用程序使用非常基本的配色方案:

  • 和的前景黑色NavigationBarTabBar
  • 白色背景_ NavigationBar_TabBar

我想为 保留相同的颜色StatusBar但事实并非如此:

  • iOS上,StatusBar颜色似乎由LightMode / DarkMode管理

=> 在不管理 DarkMode的设备上,或者当LightMode 处于活动状态时,StatusBar信息会很好地显示 iOS没有暗模式

=> 但是当DarkMode 处于活动状态时情况并非如此,因为这些信息是隐藏的 iOS - 黑暗模式

  • Android上,StatusBar颜色似乎由styles.xaml文件和android:statusBarColor属性管理

=> 如果我指定白色,则StatusBar信息不可见,因为也有白色 styles.xaml - 白色

=> 而如果我指定灰色,则StatusBar信息清晰可见 styles.xaml - 灰色

所以我尝试应用那里给出的解决方案:

  • iOS上不起作用:我仍然有相同的行为,因为StatusBar信息不可见,因为当DarkMode 处于活动状态时也有白色
  • 这似乎适用Android,但这并不涵盖所有 Android 版本(因为它适用于 Marshmallow 版本)

依赖 - 白色

=> 如何管理 iOS 状态栏的前景色?安卓够用吗?

4

2 回答 2

1

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

于 2020-12-04T03:24:03.250 回答
1

您可以对 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();
        });
    }

检查我的问题/答案

Xamarin Forms - 如何在没有导航页面的情况下更改状态栏颜色

完整的工作示例在这里https://github.com/georgemichailou/ShaXam

于 2020-12-09T07:38:29.803 回答