Skip to content

Learn Mobile Development

Alamgeer's Blog

Menu
  • Home
  • Privacy Policy
Menu
Change StatusBar Color

How to change StatusBar color in MAUI

Posted on January 22, 2023January 30, 2023 by Learn Mobile Development

MAUI Community Toolkit provides us StatusBarBehavior to change status bar color and style. So In this article, I’m going to show you how to change StatusBar color in MAUI.

Let’s Start

In this sample demo we will use CommunityToolkit.Maui nuget package.

CommunityToolkit.Maui

CommunityToolkit.Maui is a collection of Converters, Behaviours, Animations, Custom Views, Effects, Helpers etc. It simplifies the developers task when building Android, iOS, macOS and WinUI applications using MAUI.

1 – Install CommunityToolkit.Maui nuget package

Expander in MAUI

2 – In order to use the .NET MAUI Community Toolkit we need to call the extension method in MauiProgram.cs file as follows

using CommunityToolkit.Maui;
using Microsoft.Extensions.Logging;

namespace MAUIiOSStatusBarBehaviorDemo;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }
}

3 – iOS Setup

We need to add following key value to the Info.plist file.

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

We don’t require any extra setup for Android.

4 – Setting up the UI

4.1 – Create a MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="MAUIiOSStatusBarBehaviorDemo.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    x:Name="MainContainer">

    <ScrollView>
        <VerticalStackLayout Padding="30,30" Spacing="25">

            <FlexLayout
                x:Name="StackColorList"
                AlignItems="Center"
                BindableLayout.ItemsSource="{Binding Colors}"
                Direction="Row"
                FlexLayout.AlignSelf="Center"
                HorizontalOptions="FillAndExpand"
                JustifyContent="Center"
                Wrap="Wrap">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <Button
                            BackgroundColor="{Binding Color}"
                            BorderColor="Transparent"
                            BorderWidth="2"
                            Command="{Binding BindingContext.ColorItemClickedCommand, Source={x:Reference MainContainer}}"
                            CommandParameter="{Binding .}"
                            CornerRadius="0"
                            HeightRequest="100"
                            WidthRequest="100">
                            <Button.Triggers>
                                <DataTrigger
                                    Binding="{Binding IsSelected}"
                                    TargetType="Button"
                                    Value="True">
                                    <Setter Property="BorderColor" Value="Red" />
                                </DataTrigger>
                            </Button.Triggers>
                        </Button>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </FlexLayout>

            <Button
                x:Name="CounterBtn"
                Command="{Binding ChangeStatusBarColorCommand}"
                HorizontalOptions="Center"
                Text="Change StatusBar Color" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
  • As in the above MainPage.xaml I have used a bindable layout to show a list of colors and button to change the status bar color.
  • So when a user selects a color from a list and clicks on the change status bar button then that picked color will be set in the status bar.

5 – Setting up the ViewModel

using CommunityToolkit.Maui.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MAUIiOSStatusBarBehaviorDemo
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand ColorItemClickedCommand { get; set; }
        public ICommand ChangeStatusBarColorCommand { get; set; }
        private ColorItem _lastSelectedItem;

        private ObservableCollection<ColorItem> _colors;
        public ObservableCollection<ColorItem> Colors
        {
            get { return _colors; }
            set
            {
                _colors = value;
                OnPropertyChanged(nameof(Colors));
            }
        }

        public MainPageViewModel()
        {
            ColorItemClickedCommand = new Command<ColorItem>((i) => OnColorItemClicked(i));
            ChangeStatusBarColorCommand = new Command(OnChangeStatusBarColor);

            Colors = new ObservableCollection<ColorItem>()
            {
                new ColorItem(){ Color = Color.FromArgb("#F7DC6F") },
                new ColorItem(){ Color = Color.FromArgb("#7DCEA0") },
                new ColorItem(){ Color = Color.FromArgb("#7FB3D5") },
                new ColorItem(){ Color = Color.FromArgb("#9B59B6") },
                new ColorItem(){ Color = Color.FromArgb("#641E16") },
                new ColorItem(){ Color = Color.FromArgb("#00FF00") },
                new ColorItem(){ Color = Color.FromArgb("#1ABC9C") },
                new ColorItem(){ Color = Color.FromArgb("#1B4F72") },
                new ColorItem(){ Color = Color.FromArgb("#D7DBDD") },
            };
        }

        private void OnColorItemClicked(ColorItem item)
        {
            if (item != null)
            {
                if (_lastSelectedItem != null)
                    _lastSelectedItem.IsSelected = false;

                _lastSelectedItem = item;
                item.IsSelected = true;
            }
        }

        private void OnChangeStatusBarColor()
        {
            if (_lastSelectedItem != null)
            {
                CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor(_lastSelectedItem.Color);
                CommunityToolkit.Maui.Core.Platform.StatusBar.SetStyle(StatusBarStyle.LightContent);
            }
        }

        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class ColorItem : INotifyPropertyChanged
    {
        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                _isSelected = value;
                OnPropertyChanged();
            }
        }

        private Color _color;
        public Color Color
        {
            get { return _color; }
            set
            {
                _color = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
  • As in the above MainPageViewModel, I have created a list of colors and bind this to a bindable layout.
  • OnColorItemClicked() method called when a user select or pick a color from bindable layout.
  • OnChangeStatusBarColor() method gets called when a user clicks on the button. This method contains the code to change the status bar color from viewmodel.
  • We can also change status bar color from xaml and c#.
  • From xaml
<ContentPage.Behaviors>
      <toolkit:StatusBarBehavior StatusBarColor="Red" StatusBarStyle="LightContent" />
</ContentPage.Behaviors>
  • From C#
class MainPage : ContentPage
{
    public MainPage()
    {
        this.Behaviors.Add(new StatusBarBehavior
        {
            StatusBarColor = Colors.Red,
            StatusBarStyle = StatusBarStyle.LightContent
        });
    }
}

Result

That’s all for now!

You can check the full source code here.

Happy Coding! 😀

You may also like

StateContainer In MAUI

How to use StateContainer in MAUI

Expander in MAUI

How to use Expander in MAUI

Post Views: 260
Share article with

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I'm Alamgeer Ashraf and having 9+ years of experience in enterprises mobile application development with Xamarin Native, Xamarin Forms & .Net MAUI.

Archives

  • February 2023 (1)
  • January 2023 (9)
  • December 2022 (5)
  • November 2022 (6)

Latest Posts

  • Prevent Dark Mode in Xamarin forms
    How to Prevent Dark Mode in Xamarin formsFebruary 3, 2023
  • apply color to Images
    How to apply color to Images and Icons in MAUIJanuary 30, 2023
  • iOS Large Page Title
    How to set iOS large page title in MAUIJanuary 26, 2023
  • Change StatusBar Color
    How to change StatusBar color in MAUIJanuary 22, 2023
  • Item reordering to CollectionView
    How to apply item reordering to CollectionView GridItemsLayout in MAUIJanuary 18, 2023

Popular Posts

  • How to use ObservableObject in MAUI for Data Binding
  • How to use drag and drop gesture to CollectionView in MAUI
  • How to Prevent Dark Mode in Xamarin forms

Latest Comments

  1. Randyperly on How to use StateContainer in MAUIOctober 18, 2024

    very good _________________ netgame casino бездепозитный бонус

  2. SmiSew on How to use StateContainer in MAUIOctober 10, 2024

    Thanks for the post _________________ pin up casino бест

  3. StavkiTarve on How to use StateContainer in MAUIOctober 7, 2024

    interesting post _________________ слоты в 1xbet приложение

  4. Luke on How to use drag and drop gesture to CollectionView in MAUISeptember 25, 2024

    Thanks very much!

  5. Tony - Nhan Nguyen Lhoa Minh on How to apply item reordering to CollectionView GridItemsLayout in MAUIMarch 28, 2024

    That would be an amazing solution. It's easy to comprehend. Thank you very much.

Our Visitor

0 0 8 1 0 1
Users Today : 7
Users Yesterday : 12
Users Last 7 days : 42
Users Last 30 days : 131
Users This Month : 106
Total views : 14380
How to check internet connection continuously in Xamarin Forms
Trending
How to check internet connection continuously in Xamarin Forms

Category

  • .Net MAUI
  • MAUI Community Toolkit
  • MVVM
  • Xamarin Community Toolkit
  • Xamarin Forms

Contact Me

  • LinkedIn
      © 2025 Learn Mobile Development