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.
How do you set the status bar color for iOS with platform specific code on .NET MAUI?
Asked Answered
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>
This worked well, thank you. Ill just change the default colors for android. –
Messenger
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/>
This worked, ended up going the iOS specific toolkit route. Thank you for the insight. –
Messenger
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>
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;
.NET MAUI unfortunately cannot use Xamarin Namespaces. Thank you for the try though. –
Messenger
© 2022 - 2024 — McMap. All rights reserved.