Forum Xamarin.Forms

Changing text color of status bar when new page is pushed

EliasJohannesEliasJohannes ATMember ✭✭
edited November 2017 in Xamarin.Forms

Hey,

I'm trying to achieve the following:
My app usually has a black text for the status bar, but for certain screens the App changes in a full screen version with a dark background color, so i want to change the status bar text color to white. I know that in iOS i can change the color in info.plist but i don't understand how i can only change it for these certain forms pages. I researched my problem and i know that it's also possible to change the color regarding to your NavigationBar TintColor but i don't have a NavigationBar at these pages.

Any solutions or maybe a code example how i can change the status bar text color in a CustomRenderer?
Thanks in advance!

Answers

  • EliasJohannesEliasJohannes ATMember ✭✭
    Any help on this? I‘m a bit desperate with this, solution is probably so simple but i can‘t come up with one :neutral:
  • curtis.ehrhartcurtis.ehrhart USMember ✭✭

    I think it is supposed to be this, but this only seem to work on iOS

                var MainPage = new Xamarin.Forms.NavigationPage(new Pages.Page())
                {
                    BarBackgroundColor = Color.FromHex("#488FCE"),
                    BarTextColor = Color.White,
                };
    
  • NMackayNMackay GBInsider, University admin
    edited May 2018

    There is some toolbar behaviours override out of the box.

    Please check the documentation.
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/platform-specifics/

    Native Android (can override the theme in Forms)
    https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/tool-bar/replacing-the-action-bar

    Please follow these github features and bug the Forms team to push them through or help if possible.
    https://github.com/xamarin/Xamarin.Forms/issues/1716
    https://github.com/xamarin/Xamarin.Forms/issues/1719

  • nagarjunareddy.kattasnagarjunareddy.kattas USMember ✭✭
    edited May 2018

    We can achieve this using Effects in Xamarin forms.

    In the shared project

    public class StatusBarEffect : RoutingEffect
    {
    private static Color BackgroundColor;
    public static void SetBackgroundColor(Color color)
    {
    BackgroundColor = color;
    }
    public static Color GetBackgroundColor()
    {
    return BackgroundColor;
    }

         public StatusBarEffect() : base ("Xamarin.StatusBarEffect")
         {}
     }
    

    Code for iOS

    assembly: ResolutionGroupName("Xamarin")]
    [assembly: ExportEffect(typeof(MyStatusBarEffect), "StatusBarEffect")]

    namespace yournamespce
    {
    public class MyStatusBarEffect : PlatformEffect
    {
    protected override void OnAttached()
    {
    UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
    if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
    {
    statusBar.BackgroundColor = StatusBarEffect.GetBackgroundColor().ToUIColor();
    }
    }

         protected override void OnDetached()
         {
    
         }
     }
    

    }

    you can use the statusbar effect as follows in the onappearing method:

    protected override void OnAppearing()
    {
    base.OnAppearing();
    StatusBarEffect.SetBackgroundColor(Color.Green);
    this.Effects.Add(new StatusBarEffect());
    }

    This solution mainly depends on Jorge Ramirez post.

Sign In or Register to comment.