Search code examples
.netmacosmauititlebarmac-catalyst

How to hide the title bar on the Mac Catalyst version of my MAUI program?


I am just getting into .NET and MAUI, and I wanted to remove the titlebar on this test app. I am not talking about the toolbar created by MAUI (in light grey), but rather the window title bar set my UIKit. This Apple Developers page shows what I want to do.

I have tried changing the title bar visibility in the program.cs inside the MacCatalyst folder (as was suggested here for a slightly unrelated problem), and I do have access to the UIKit.UITitleBar class (and can access its visibility state with the UIKit.UITitlebarTitleVisibility enum), but I have not found a way to turn to change this property.


Solution

  • You need to override the CreateWindow method in your App class to create a Window, I created a new project to test on Mac, you can refer to the following code:

    using UIKit;
    
    namespace MauiAppDemo;
    
    public partial class App : Application
    
    {    
      public App()    
      {        
        InitializeComponent();        
        //MainPage = new AppShell();             
      }
    
      protected override Window CreateWindow(IActivationState activationState)    
      {
        var window = new Window();        
        window.HandlerChanged += windowHandlerChanged;        
        var rootPage = new MainPage();        
        window.Page = rootPage;        
        return window;    
      }
    
      private void windowHandlerChanged(object sender, EventArgs e)    
      {        
        var win = sender as Microsoft.Maui.Controls.Window;        
        var uiWin = win.Handler.PlatformView as UIWindow;        
        if (uiWin != null)        
        {            
            uiWin.WindowScene.Titlebar.TitleVisibility = UITitlebarTitleVisibility.Hidden;        
        }   
      }
    }
    

    Hope it helps.