Home > front end >  How do you set the status bar color for iOS with platform specific code on .NET MAUI?
How do you set the status bar color for iOS with platform specific code on .NET MAUI?

Time:12-22

I am looking to set the status bar color for iOS on .NET MAUI. I have tried a combination of Community Toolkit (Current bug where release builds of Android crash on startup), Background Color and Background (Both not respected by iOS on startup) and setting the color myself on a shape I drew behind the status bar and that didn't work to well, a little finicky with device orientation. I have tried googling and have not found much on how to use UIKit to set the color. Any help is much appreciated.

CodePudding user response:

To set the status bar color for iOS with platform specific code in .NET MAUI you can use the StatusBar class.

Example could be:

using Xamarin.Forms; 
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;

// ...

var statusBar = new StatusBar();

statusBar.GetForCurrentView().BackgroundColor = Colors.Red;

CodePudding user response:

The easiest way I am aware of to do this is you override the finishedLaunching method and change the colour:

 public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
        if (statusBar != null && statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
        {
            statusBar.BackgroundColor =UIColor.Yellow;
        }
        return base.FinishedLaunching(application, launchOptions);
    }

In your info.plist you need to have this setting:

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

Goodluck

CodePudding user response:

You can use .NET MAUI CommunityToolkit package to set the set the status bar color for iOS.

XAML usage:

In order to make use of the toolkit within XAML you can use this namespace:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

And then add below to your XAML page:

 <ContentPage.Behaviors> 
        <toolkit:StatusBarBehavior StatusBarColor="Red" StatusBarStyle="LightContent"> 
        </toolkit:StatusBarBehavior>
 </ContentPage.Behaviors>

  • Related