首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin.Forms.Shell:如何管理StatusBar颜色

Xamarin.Forms.Shell:如何管理StatusBar颜色
EN

Stack Overflow用户
提问于 2020-12-03 18:36:35
回答 2查看 1.6K关注 0票数 2

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

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

  • 黑色表示NavigationBarTabBar前台
  • white表示背景 of NavigationBar TabBar

我希望StatusBar保留相同的颜色,但事实并非如此:

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

=>在不管理DarkMode的设备上,或者当LightMode是active时,StatusBar信息显示得很好

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

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

=>如果我指定了白色颜色,则StatusBar信息是不可见的,因为也有白色。

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

所以我试着应用一个给定那里的解决方案

  • 这不适用于 on iOS:我仍然有相同的行为,因为当DarkMode是活动时,StatusBar信息是不可见的,因为也有白色信息
  • 这似乎适用于 Android 上的,但这并不包括所有的安卓版本(因为它是从Marshmallow版本运行的)。

=>如何管理iOS StatusBar前景色?对Android来说足够了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-09 07:38:29

您可以对iOS (带有黑色文本的白色状态栏)使用以下方法

代码语言:javascript
复制
    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();
        });
    }

核对我的问题/答案

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

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

票数 2
EN

Stack Overflow用户

发布于 2020-12-04 03:24:03

通过在这条线.中的解决方案,操作系统解决了这个问题。

代码语言:javascript
复制
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在不同模式下设置不同的颜色。

代码语言:javascript
复制
<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>

如果要在不同的应用主题下保持状态栏颜色不变,请使用自定义呈现器:

代码语言:javascript
复制
[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添加了一个键:

代码语言:javascript
复制
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

参考:更喜欢这种风格

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65132070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档