{"id":771,"date":"2023-01-22T17:12:13","date_gmt":"2023-01-22T17:12:13","guid":{"rendered":"https:\/\/learnmobiledevelopment.com\/?p=771"},"modified":"2023-01-30T15:39:49","modified_gmt":"2023-01-30T15:39:49","slug":"change-statusbar-color-in-maui","status":"publish","type":"post","link":"https:\/\/learnmobiledevelopment.com\/index.php\/2023\/01\/22\/change-statusbar-color-in-maui\/","title":{"rendered":"How to change StatusBar color in MAUI"},"content":{"rendered":"\n<p><strong><strong>MAUI<\/strong><\/strong>&nbsp;<strong>Community Toolkit<\/strong> provides us <strong>StatusBarBehavior<\/strong> to change status bar color and style. So In this article, I\u2019m going to show you how to change StatusBar color in MAUI.<\/p>\n\n\n\n<p style=\"font-size:25px\"><strong>Let\u2019s Start<\/strong><\/p>\n\n\n\n<p>In this sample demo we will use&nbsp;<strong><a href=\"https:\/\/www.nuget.org\/packages\/CommunityToolkit.Maui\/\" target=\"_blank\" rel=\"noreferrer noopener\">CommunityToolkit.Maui<\/a><\/strong>&nbsp;nuget package.<\/p>\n\n\n\n<p><strong>CommunityToolkit.Maui<\/strong><\/p>\n\n\n\n<p>CommunityToolkit.Maui&nbsp;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.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>1 \u2013 Install CommunityToolkit.Maui&nbsp;nuget package<\/strong><\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"606\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-1024x606.png\" alt=\"Expander in MAUI\" class=\"wp-image-422\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-1024x606.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-300x178.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-768x455.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-1536x909.png 1536w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529-850x503.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/ExpanderinMAUI-e1670421322529.png 1802w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>2 \u2013 In order to use the .NET MAUI Community Toolkit we need to call the extension method in MauiProgram.cs&nbsp;file as follows<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using CommunityToolkit.Maui;\nusing Microsoft.Extensions.Logging;\n\nnamespace MAUIiOSStatusBarBehaviorDemo;\n\npublic static class MauiProgram\n{\n    public static MauiApp CreateMauiApp()\n    {\n        var builder = MauiApp.CreateBuilder();\n        builder\n            .UseMauiApp&lt;App>()\n            .UseMauiCommunityToolkit()\n            .ConfigureFonts(fonts =>\n            {\n                fonts.AddFont(\"OpenSans-Regular.ttf\", \"OpenSansRegular\");\n                fonts.AddFont(\"OpenSans-Semibold.ttf\", \"OpenSansSemibold\");\n            });\n\n#if DEBUG\n        builder.Logging.AddDebug();\n#endif\n\n        return builder.Build();\n    }\n}<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3 &#8211; iOS Setup<\/strong><\/p>\n\n\n\n<p>We need to add following key value to the <strong>Info.plist<\/strong> file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> &lt;key>UIViewControllerBasedStatusBarAppearance&lt;\/key>\n &lt;false\/><\/pre>\n\n\n\n<p>We don\u2019t require any extra setup for <strong>Android<\/strong>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>4 \u2013 Setting up the UI<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>4.1 \u2013 Create a MainPage.xaml<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?>\n&lt;ContentPage\n    x:Class=\"MAUIiOSStatusBarBehaviorDemo.MainPage\"\n    xmlns=\"http:\/\/schemas.microsoft.com\/dotnet\/2021\/maui\"\n    xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"\n    xmlns:toolkit=\"http:\/\/schemas.microsoft.com\/dotnet\/2022\/maui\/toolkit\"\n    x:Name=\"MainContainer\">\n\n    &lt;ScrollView>\n        &lt;VerticalStackLayout Padding=\"30,30\" Spacing=\"25\">\n\n            &lt;FlexLayout\n                x:Name=\"StackColorList\"\n                AlignItems=\"Center\"\n                BindableLayout.ItemsSource=\"{Binding Colors}\"\n                Direction=\"Row\"\n                FlexLayout.AlignSelf=\"Center\"\n                HorizontalOptions=\"FillAndExpand\"\n                JustifyContent=\"Center\"\n                Wrap=\"Wrap\">\n                &lt;BindableLayout.ItemTemplate>\n                    &lt;DataTemplate>\n                        &lt;Button\n                            BackgroundColor=\"{Binding Color}\"\n                            BorderColor=\"Transparent\"\n                            BorderWidth=\"2\"\n                            Command=\"{Binding BindingContext.ColorItemClickedCommand, Source={x:Reference MainContainer}}\"\n                            CommandParameter=\"{Binding .}\"\n                            CornerRadius=\"0\"\n                            HeightRequest=\"100\"\n                            WidthRequest=\"100\">\n                            &lt;Button.Triggers>\n                                &lt;DataTrigger\n                                    Binding=\"{Binding IsSelected}\"\n                                    TargetType=\"Button\"\n                                    Value=\"True\">\n                                    &lt;Setter Property=\"BorderColor\" Value=\"Red\" \/>\n                                &lt;\/DataTrigger>\n                            &lt;\/Button.Triggers>\n                        &lt;\/Button>\n                    &lt;\/DataTemplate>\n                &lt;\/BindableLayout.ItemTemplate>\n            &lt;\/FlexLayout>\n\n            &lt;Button\n                x:Name=\"CounterBtn\"\n                Command=\"{Binding ChangeStatusBarColorCommand}\"\n                HorizontalOptions=\"Center\"\n                Text=\"Change StatusBar Color\" \/>\n\n        &lt;\/VerticalStackLayout>\n    &lt;\/ScrollView>\n\n&lt;\/ContentPage><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As in the above <strong>MainPage.xaml<\/strong> I have used a bindable layout to show a list of colors and button to change the status bar color.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ul>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>5 &#8211; Setting up the ViewModel<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using CommunityToolkit.Maui.Core;\nusing System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.ComponentModel;\nusing System.Linq;\nusing System.Runtime.CompilerServices;\nusing System.Text;\nusing System.Threading.Tasks;\nusing System.Windows.Input;\n\nnamespace MAUIiOSStatusBarBehaviorDemo\n{\n    public class MainPageViewModel : INotifyPropertyChanged\n    {\n        public event PropertyChangedEventHandler PropertyChanged;\n\n        public ICommand ColorItemClickedCommand { get; set; }\n        public ICommand ChangeStatusBarColorCommand { get; set; }\n        private ColorItem _lastSelectedItem;\n\n        private ObservableCollection&lt;ColorItem> _colors;\n        public ObservableCollection&lt;ColorItem> Colors\n        {\n            get { return _colors; }\n            set\n            {\n                _colors = value;\n                OnPropertyChanged(nameof(Colors));\n            }\n        }\n\n        public MainPageViewModel()\n        {\n            ColorItemClickedCommand = new Command&lt;ColorItem>((i) => OnColorItemClicked(i));\n            ChangeStatusBarColorCommand = new Command(OnChangeStatusBarColor);\n\n            Colors = new ObservableCollection&lt;ColorItem>()\n            {\n                new ColorItem(){ Color = Color.FromArgb(\"#F7DC6F\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#7DCEA0\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#7FB3D5\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#9B59B6\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#641E16\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#00FF00\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#1ABC9C\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#1B4F72\") },\n                new ColorItem(){ Color = Color.FromArgb(\"#D7DBDD\") },\n            };\n        }\n\n        private void OnColorItemClicked(ColorItem item)\n        {\n            if (item != null)\n            {\n                if (_lastSelectedItem != null)\n                    _lastSelectedItem.IsSelected = false;\n\n                _lastSelectedItem = item;\n                item.IsSelected = true;\n            }\n        }\n\n        private void OnChangeStatusBarColor()\n        {\n            if (_lastSelectedItem != null)\n            {\n                CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor(_lastSelectedItem.Color);\n                CommunityToolkit.Maui.Core.Platform.StatusBar.SetStyle(StatusBarStyle.LightContent);\n            }\n        }\n\n        public void OnPropertyChanged([CallerMemberName] string propertyName = null)\n        {\n            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));\n        }\n    }\n\n    public class ColorItem : INotifyPropertyChanged\n    {\n        private bool _isSelected;\n        public bool IsSelected\n        {\n            get { return _isSelected; }\n            set\n            {\n                _isSelected = value;\n                OnPropertyChanged();\n            }\n        }\n\n        private Color _color;\n        public Color Color\n        {\n            get { return _color; }\n            set\n            {\n                _color = value;\n                OnPropertyChanged();\n            }\n        }\n\n        public event PropertyChangedEventHandler PropertyChanged;\n        public void OnPropertyChanged([CallerMemberName] string propertyName = null)\n        {\n            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));\n        }\n    }\n}<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As in the above <strong>MainPageViewModel<\/strong>, I have created a list of colors and bind this to a bindable layout.<\/li>\n\n\n\n<li><strong>OnColorItemClicked()<\/strong> method called when a user select or pick a color from bindable layout.<\/li>\n\n\n\n<li><strong><strong>OnChangeStatusBarColor()<\/strong> <\/strong>method gets called when a user clicks on the button. This method contains the code to change the status bar color from viewmodel.<\/li>\n\n\n\n<li>We can also change status bar color from <strong>xaml<\/strong> and <strong>c#<\/strong>.<\/li>\n<\/ul>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>From xaml<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;ContentPage.Behaviors>\n      &lt;toolkit:StatusBarBehavior StatusBarColor=\"Red\" StatusBarStyle=\"LightContent\" \/>\n&lt;\/ContentPage.Behaviors><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>From C#<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class MainPage : ContentPage\n{\n    public MainPage()\n    {\n        this.Behaviors.Add(new StatusBarBehavior\n        {\n            StatusBarColor = Colors.Red,\n            StatusBarStyle = StatusBarStyle.LightContent\n        });\n    }\n}<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Result <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"2796\" style=\"aspect-ratio: 1290 \/ 2796;\" width=\"1290\" controls src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/ChangeStatusBarColor.mp4\"><\/video><\/figure>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>That\u2019s all for now!<\/p>\n\n\n\n<p>You can check the full source code <a href=\"https:\/\/github.com\/Alam-Ashraf\/MAUIiOSStatusBarBehaviorDemo\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a><strong>.<\/strong><\/p>\n\n\n\n<p>Happy Coding!<strong> \ud83d\ude00<\/strong><\/p>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>You may also like<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-0dfbf163 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2022\/12\/11\/statecontainer-in-maui\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/StateContainerInMAUI-1024x789.png\" alt=\"StateContainer In MAUI\" class=\"wp-image-481\" width=\"512\" height=\"395\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/StateContainerInMAUI-1024x789.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/StateContainerInMAUI-300x231.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/StateContainerInMAUI-768x592.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/StateContainerInMAUI-850x655.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/StateContainerInMAUI.png 1176w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to use StateContainer in MAUI<\/p>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2022\/12\/03\/expander-in-maui\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/Expander-in-Dot-Net-MAUI-1024x624.png\" alt=\"Expander in MAUI\" class=\"wp-image-435\" width=\"512\" height=\"312\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/Expander-in-Dot-Net-MAUI-1024x624.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/Expander-in-Dot-Net-MAUI-300x183.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/Expander-in-Dot-Net-MAUI-768x468.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/Expander-in-Dot-Net-MAUI-850x518.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/Expander-in-Dot-Net-MAUI.png 1260w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to use Expander in MAUI<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MAUI&nbsp;Community Toolkit provides us StatusBarBehavior to change status bar color and style. So In this article, I\u2019m going to show you how to change StatusBar color in MAUI. Let\u2019s Start In this sample demo we will use&nbsp;CommunityToolkit.Maui&nbsp;nuget package. CommunityToolkit.Maui CommunityToolkit.Maui&nbsp;is a collection of Converters, Behaviours, Animations, Custom Views, Effects, Helpers etc. It simplifies the developers&#8230;<\/p>\n","protected":false},"author":1,"featured_media":782,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,37],"tags":[61,62],"class_list":["post-771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnetmaui","category-maui-community-toolkit","tag-how-to-change-status-bar-color","tag-status-bar-color-in-maui"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/comments?post=771"}],"version-history":[{"count":13,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":803,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/771\/revisions\/803"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media\/782"}],"wp:attachment":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}