1

How to Change Status Bar Background, Text Color for Single Content Page.

I add:

styles.xml

enter image description here

I think it works on all Content Pages

enter image description here

I want:

Page1: Status Bar Background is Green.

Page2: Status Bar Background is Red. ...

Thank you!

Chim Di Tru
  • 495
  • 4
  • 17
  • 2
    A similar question has been answered here https://stackoverflow.com/questions/37993741/xamarin-forms-change-statusbar-color – Shubham Tyagi Aug 16 '21 at 12:06

2 Answers2

3

How to Change Status Bar Background, Text Color for Single Content Page Xamarin (Ios, Android)

If you want to change the Status Bar Background color for single page, you can use DependencyService to achieve this.

1.In xamarin forms,add interface IStatusBarPlatformSpecific

public interface IStatusBarPlatformSpecific
{
    void SetStatusBarColor(Color color);
}

2.In android,add class ChangeStatusbar to implement interface IStatusBarPlatformSpecific:

[assembly: Dependency(typeof(ChangeStatusbar))]
namespace ToastApp.Droid
{
    public class ChangeStatusbar : IStatusBarPlatformSpecific
    {
        public ChangeStatusbar()
        {
        }

        public void SetStatusBarColor(Xamarin.Forms.Color color)
        {
            // The SetStatusBarcolor is new since API 21
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                var androidColor = color.AddLuminosity(-0.1).ToAndroid();
                //Just use the plugin
                Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(androidColor);
            }
            else
            {
                // Here you will just have to set your 
                // color in styles.xml file as shown below.
            }
        }
    }
}

Note:Android only supports black or white icons & text.

For more, check:How to customize status bar icons and text color? E.g. status bar background: white, status bar icon color, and text: red

3.In IOS,create class ChangeStatusbarIOS to implement interface IStatusBarPlatformSpecific:

[assembly: Dependency(typeof(ChangeStatusbarIOS))]
namespace ToastApp.iOS
{
    public class ChangeStatusbarIOS : IStatusBarPlatformSpecific
    {
        public void SetStatusBarColor(Color color)
        {

            UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
            //statusBar.BackgroundColor = UIColor.Yellow;

            statusBar.BackgroundColor = color.AddLuminosity(-0.1).ToUIColor();
            UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
        }
    }
}

4.In forms, you can change status backbround color for Single Content Page by override method OnAppearing and change back to it's original color by override method OnDisappearing:

    protected override void OnAppearing()
    {
        base.OnAppearing();

        var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
        statusbar.SetStatusBarColor(Color.Green);
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
        statusbar.SetStatusBarColor(Color.FromHex("3F51B5"));
    }
Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • On Android it works fine, but on IOS it gives an error System.NullReferenceException: 'Object reference not set to an instance of an object'. Do you have any suggestions? – Chim Di Tru Oct 22 '21 at 09:02
0

on ios:

[assembly: Dependency(typeof(ChangeStatusbarIOS))]
namespace ToastApp.iOS
{
    public class ChangeStatusbarIOS : IStatusBarPlatformSpecific
    {
        public void SetStatusBarColor(Color color)
        {

            UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
            //statusBar.BackgroundColor = UIColor.Yellow;

            statusBar.BackgroundColor = color.AddLuminosity(-0.1).ToUIColor();
            //UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);

                foreach (UIScene scene in UIApplication.SharedApplication.ConnectedScenes)
            {
                if (scene.ActivationState == UISceneActivationState.ForegroundActive)
                {
                    UIWindowScene myScene = (UIWindowScene)scene;
                    foreach (UIWindow win in myScene.Windows)
                    {
                        if (win.IsKeyWindow)
                        {
                            win.AddSubview(statusBar);
                        }
                    }

                }
            }

        }
    }
}