Skip to content

This repository contains a sample explaining how to center align tabs in .NET MAUI TabView for Windows and Mac platforms. Tokens: maui, maui-tab-view, tab-view, tab-alignment

Notifications You must be signed in to change notification settings

SyncfusionExamples/How-to-Center-Align-Tabs-in-.NET-MAUI-TabView-for-Windows-and-Mac-platforms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

This article guides you on how to center align tabs in .NET MAUI TabView for Windows and Mac platforms.

To customize the placement of tabs to the center of the Windows and Mac screens, set TabWidthMode to SizeToContent and adjust TabHeaderPadding dynamically based on the screen's width. Below is a code snippet demonstrating this approach.

XAML Code

<tabView:SfTabView TabWidthMode="SizeToContent">
…
<\tabView:SfTabView>

C#

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

#if WINDOWS
        var window = GetParentWindow().Handler.PlatformView as MauiWinUIWindow;
        if (window != null)
        {
            UpdateTabHeaderPadding(window.Bounds.Width);
        }
#elif MACCATALYST
        var macWindow = GetParentWindow().Handler.PlatformView as UIWindow;
        if (macWindow != null)
        {
            UpdateTabHeaderPadding(macWindow.Bounds.Width);
        }
#endif
    }

#if WINDOWS || MACCATALYST
    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
        UpdateTabHeaderPadding(width);
    }
    private void UpdateTabHeaderPadding(double width)
    {
        double totalTabWidth = 0;
        foreach (var tabItem in tabView.Items)
        {
            if (tabItem is Syncfusion.Maui.TabView.SfTabItem tabItemView)
            {
                totalTabWidth += tabItemView.Measure(double.PositiveInfinity, double.PositiveInfinity).Request.Width;
            }
        }
        double remainingSpace = ((width - totalTabWidth));

        double padding = (remainingSpace / 2) ;

        tabView.TabHeaderPadding = new Thickness(padding, 0, 0, 0);
    }
#endif
}
  • The TabWidthMode is set to SizeToContent so that each tab's width is adjusted based on its content.
  • The method UpdateTabHeaderPadding calculates the total width of all the tab items and dynamically adjusts the left padding of the tab headers to center them based on the screen width.

Output

TabView_TabAlignment.gif

Requirements to run the demo

To run the demo, refer to System Requirements for .NET MAUI

Troubleshooting:

Path too long exception

If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.

About

This repository contains a sample explaining how to center align tabs in .NET MAUI TabView for Windows and Mac platforms. Tokens: maui, maui-tab-view, tab-view, tab-alignment

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages