5

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.

4 Answers 4

11

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>

Sign up to request clarification or add additional context in comments.

1 Comment

This worked well, thank you. Ill just change the default colors for android.
2

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/>

1 Comment

This worked, ended up going the iOS specific toolkit route. Thank you for the insight.
1

Change PageBackgroundColor key in the shared top level file App.xaml


This line: <Color x:Key="PageBackgroundColor">White</Color>

<?xml version="1.0" encoding="UTF-8"?>

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiBlazorApp.App">
    <Application.Resources>
        <ResourceDictionary>

            <!-- HERE! -->
            <Color x:Key="PageBackgroundColor">White</Color>
            <Color x:Key="PrimaryTextColor">Black</Color>

            <Style TargetType="Label">
                <Setter Property="TextColor" Value="{DynamicResource PrimaryTextColor}" />
                <Setter Property="FontFamily" Value="OpenSansRegular" />
            </Style>

            <Style TargetType="Button">
                <Setter Property="TextColor" Value="{DynamicResource PrimaryTextColor}" />
                <Setter Property="FontFamily" Value="OpenSansRegular" />
                <Setter Property="BackgroundColor" Value="#2b0b98" />
                <Setter Property="Padding" Value="14,10" />
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>

Comments

-2

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;

1 Comment

.NET MAUI unfortunately cannot use Xamarin Namespaces. Thank you for the try though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.