【问题标题】:Xamarin.Forms.Shell: how to manage StatusBar colorXamarin.Forms.Shell:如何管理 StatusBar 颜色
【发布时间】:2020-12-03 18:36:35
【问题描述】:

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

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

  • 黑色代表NavigationBar和TabBar的前景
  • 白色代表NavigationBar和TabBar的背景

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

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

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

=> 但当 DarkMode 处于活动状态 时并非如此,因为这些信息是隐藏的

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

=> 如果我指定 white 颜色,StatusBar 信息不可见,因为也有白色

=> 而如果我指定 gray 颜色,则StatusBar 信息清晰可见

所以我尝试应用给定there 的解决方案:

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

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

【问题讨论】:

    标签: android ios xamarin.forms statusbar xamarin.forms.shell


    【解决方案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 - how to change the status bar color without a navigation page

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

    【讨论】:

    • 嗨@G.Mich 感谢您的评论。我在描述中提到了你的解决方案,但我不知道我是否可以复制答案:正如你所做的那样,它是完美的!
    【解决方案2】:

    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

    【讨论】:

    • 嗨@Jack Hua - MSFT,感谢您的回答。我的应用不支持DarkMode,因为它在WebView 中嵌入了一个外部URL,它不能管理DarkMode。我已经尝试过您的建议,但它不起作用:我仍然有隐藏的StatusBar 的信息...
    • 嗯,它对我有用。尝试返回 UIStatusBarStyle.DarkContent;。您在哪个设备和系统中进行测试?
    • 我在 iPhone XS 和 iPhone 12 Pro 模拟器 上进行了测试。最后,我应用了G.Mich 给出的解决方案的更新。我不知道为什么您的解决方案对我不起作用:可能是因为我使用了Shell?
    • 好的,您可以在答案中分享您的解决方案并标记它,这将帮助更多有相同问题的人:)。
    • 最后,我在描述中分享的 URL 中给出的解决方案已经更新,现在可以在 iOS 上运行。我需要复制它吗?
    猜你喜欢
    • 2011-04-15
    • 2018-09-03
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多