Dynamically change status bar text color on Xamarin Forms iOS

RomaneDonnetRomaneDonnet ✭✭CHMember ✭✭

Hi guys!

I'm having some trouble changing the status bar text color dynamically in my app. I know that there is an info.plist property to modify it for the whole application but I wanted to change it dynamically. For instance click a button and change from dark to light.

Is it possible?

Answers

  • JohnHardmanJohnHardman mod GBUniversity mod
    edited November 2016

    @RomaneDonnet - There may be a better way, but one way would be to set app.StatusBarStyle in OnAppearing and change it back again in OnDisappearing.

            app.StatusBarStyle = UIStatusBarStyle.LightContent;
    

    (I haven't tested this, but would expect it to work)

  • RomaneDonnetRomaneDonnet ✭✭ CHMember ✭✭

    Thanks for the quick answer! But what the "app." stands for? is it a link to the main App.xaml.cs class? I tried to go there and put this.Status... but there's nothing...

    Any clue?

  • JohnHardmanJohnHardman mod GBUniversity mod

    @RomaneDonnet - app is the UIApplication that is passed to FinishedLaunching in AppDelegate.cs

  • RomaneDonnetRomaneDonnet ✭✭ CHMember ✭✭
    edited November 2016

    Hi thanks again! I managed to code thing right with a native interface to call the native function from Xamarin Forms the code is pretty clean but still the function

    app.StatusBarStyle = UIStatusBarStyle.LightContent;

    nor

    app.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);

    seems to work... I tried to call it directly on the FinishedLaunching in AppDelegate to see if the problem was the moment of the call but still... nothings change

  • JohnHardmanJohnHardman mod GBUniversity mod

    @RomaneDonnet - Interesting. I call app.StatusBarStyle = UIStatusBarStyle.LightContent; from within FinishedLaunching and it works there. I wasn't 100% sure whether you could call it later on - I'll give it a go when I get a moment and will let you know what I find.

    What version of the Xamarin Forms Nuget package are you using?

  • RomaneDonnetRomaneDonnet ✭✭ CHMember ✭✭
    edited November 2016

    Xamarin.Forms (2.3.3.168) on both PCL and iOS project.

    I made a blank solution and just changed

    [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); app.StatusBarStyle = UIStatusBarStyle.LightContent; return base.FinishedLaunching(app, options); } }

    Still not working

  • JohnHardmanJohnHardman mod GBUniversity mod
    edited November 2016

    @RomaneDonnet - It seems that NavigationRenderer now does the following, so assuming you are using NavigationPage just setting the BarTextColor property will now change the status bar color, presumably overriding what was set earlier. Guessing that must be a fairly recent change, but haven't checked.

            if (barTextColor.Luminosity > 0.5)
            {
                // Use light text color for status bar
                UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
            }
            else
            {
                // Use dark text color for status bar
                UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.Default;
            }
    
  • RomaneDonnetRomaneDonnet ✭✭ CHMember ✭✭
    edited November 2016

    I'm starting to get really concern about whats happening here :D

    NavigationPage.BarTextColor doesn't change status bar color for iOS

    public partial class App : Application{ public App() { InitializeComponent(); NavigationPage np = new NavigationPage(new StatusBarPage()); np.BarBackgroundColor = Color.Blue; np.BarTextColor = Color.White; MainPage = np; } }

    I started another solution from scratch and still black text. I attached the result

    I also tried using the SharedApplication property but doesn't work ....

    I'm really sorry to bother you .... thanks for all your answers

  • RomaneDonnetRomaneDonnet ✭✭ CHMember ✭✭

    Up, please I need some help!

  • JohnHardmanJohnHardman mod GBUniversity mod

    @RomaneDonnet - That bit from the NavigationRenderer switches between two styles, choosing which based on the luminosity of the text color that has been specified. If you take the two extremes of Color.White and Color.Black as the NavigationPage.BarTextColor, do you see the status bar change?

  • RomaneDonnetRomaneDonnet ✭✭ CHMember ✭✭

    @JohnHardman - Thanks for your answer! I tried Black or blue background, black or white text color and still no change appear in the App

  • RomaneDonnetRomaneDonnet ✭✭ CHMember ✭✭

    The Navigation Bar text color changes correctly but not the status bar :/

  • JohnHardmanJohnHardman mod GBUniversity mod

    @RomaneDonnet - Can you create the smallest solution possible that shows the problem, clean it (so no obj, bin etc) and post it to github, dropbox or similar. I'm in the midst of setting up in-app billing at the moment, but will try to take a look when I get time.

  • LeoJHarrisLeoJHarris ✭✭✭ NZMember ✭✭✭

    This works, the background and text color changes applied to the status bar.

    In App delegate:

    LoadApplication(new App());
    
                UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
                if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
                {
                    statusBar.BackgroundColor = UIColor.Black;
                    statusBar.TintColor = UIColor.White;
                }
    
                return base.FinishedLaunching(app, options);
    

    And add this to info.plist:

        <key>UIViewControllerBasedStatusBarAppearance</key>
            <false/>
            <key>UIStatusBarStyle</key>
            <string>UIStatusBarStyleBlackOpaque</string>
    
Sign In or Register to comment.