JakenBake
JakenBake

Reputation: 113

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

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.

Upvotes: 5

Views: 5115

Answers (4)

Moffen
Moffen

Reputation: 2003

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>

Upvotes: 1

FreakyAli
FreakyAli

Reputation: 16547

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

Upvotes: 2

Alexandar May - MSFT
Alexandar May - MSFT

Reputation: 10148

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>

Upvotes: 11

naik899
naik899

Reputation: 116

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;

Upvotes: -2

Related Questions