Forum Xamarin.Forms

Xamarin.Forms iOS change StatusBar font to white

Hello,
I need to set white font of iOS top status bar on some pages - programatically.

I also need to have set UIViewControllerBasedStatusBarAppearance to YES in Info.plist, because I need to hide status bar on some pages.

I tried create custom render on my MasterDetailPage and here override PreferredStatusBarStyle, but it is not working...
Function PreferredStatusBarStyle is never called...

I also tried this.NavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
But it is not working NavigationController is always null.

Is there a way how to change color of StatusBar programatically when using Xamarin.Form?

Tagged:

Posts

  • veryhumbleveryhumble USMember ✭✭

    Hi,

    Not sure if I understand your problem correctly. Our app has some dark and some light pages, which means we want a white status bar on a dark page. We created a custom ContentPage for this called ContentPageEx which has a property StatusBarStyle, this can either be Light or Dark and is set in the XAML Page. In the Renderer we just use ViewWillAppear override to set the status bar like this:

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
    
        var barStyle = UIStatusBarStyle.Default;
        switch (_barStyle)
        {
            case ContentPageExStatusBarStyle.Default:
                barStyle = UIStatusBarStyle.Default;
                break;
            case ContentPageExStatusBarStyle.Light:
                barStyle = UIStatusBarStyle.LightContent;
                break;
            case ContentPageExStatusBarStyle.Dark:
                barStyle = UIStatusBarStyle.BlackTranslucent;
                break;
        }
    
        UIApplication.SharedApplication.SetStatusBarStyle(barStyle, animated);
    }
    
    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);
        UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.Default, animated);
    }
    

    in the info.plist, i have set:

    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    

    hope this helps

  • MichalSamanMichalSaman USMember

    @veryhumble Thanks, problem is, that your solution works only when UIViewControllerBasedStatusBarAppearance is set to false.
    I need UIViewControllerBasedStatusBarAppearance = true, because some viewControllers of 3rd party components, that I use in project, also changing visibility of StatusBar and it only works with enabled UIViewControllerBasedStatusBarAppearance.

Sign In or Register to comment.