Skip to content

Commit

Permalink
Merge pull request #1 from SyncfusionExamples/Sample
Browse files Browse the repository at this point in the history
Added the sample for Tab View KB.
  • Loading branch information
naveenkumar-sanjeevirayan authored Nov 1, 2024
2 parents 84539be + bab81af commit df13c81
Show file tree
Hide file tree
Showing 37 changed files with 1,233 additions and 2 deletions.
85 changes: 83 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,83 @@
# How-to-Center-Align-Tabs-in-.NET-MAUI-TabView-for-Windows-and-Mac-platforms
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
This article guides you on how to center align tabs in [.NET MAUI TabView](https://www.syncfusion.com/maui-controls/maui-tab-view) for Windows and Mac platforms.

To customize the placement of tabs to the center of the Windows and Mac screens, set [TabWidthMode](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html#Syncfusion_Maui_TabView_SfTabView_TabWidthMode) to [SizeToContent](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.TabWidthMode.html#Syncfusion_Maui_TabView_TabWidthMode_SizeToContent) and adjust [TabHeaderPadding](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html#Syncfusion_Maui_TabView_SfTabView_TabHeaderPadding) dynamically based on the screen's width. Below is a code snippet demonstrating this approach.

**XAML Code**

```xaml
<tabView:SfTabView TabWidthMode="SizeToContent">
<\tabView:SfTabView>
```

**C#**

```csharp
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](https://support.syncfusion.com/kb/agent/attachment/article/17398/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjI5MzYwIiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.99mAjs80Wwjabr15ABNoC5JX8l7RrCo36DsNBR5P4is)

**Requirements to run the demo**

To run the demo, refer to [System Requirements for .NET MAUI](https://help.syncfusion.com/maui/system-requirements)

**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.
31 changes: 31 additions & 0 deletions TabViewSample/TabViewSample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TabViewSample", "TabViewSample\TabViewSample.csproj", "{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Release-Xml|Any CPU = Release-Xml|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release|Any CPU.Build.0 = Release|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release|Any CPU.Deploy.0 = Release|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release-Xml|Any CPU.ActiveCfg = Release|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release-Xml|Any CPU.Build.0 = Release|Any CPU
{E377EC6C-6773-417F-9ED7-7566F6F0D6BB}.Release-Xml|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B69CA809-EB64-4829-B9A8-BA7F393E582F}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions TabViewSample/TabViewSample/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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"
xmlns:local="clr-namespace:TabViewSample"
x:Class="TabViewSample.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
12 changes: 12 additions & 0 deletions TabViewSample/TabViewSample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TabViewSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
}
15 changes: 15 additions & 0 deletions TabViewSample/TabViewSample/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="TabViewSample.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TabViewSample"
Shell.FlyoutBehavior="Disabled"
Title="TabViewSample">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions TabViewSample/TabViewSample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TabViewSample
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
99 changes: 99 additions & 0 deletions TabViewSample/TabViewSample/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TabViewSample.MainPage"
xmlns:tabView="clr-namespace:Syncfusion.Maui.TabView;assembly=Syncfusion.Maui.TabView">
<StackLayout > <tabView:SfTabView x:Name="tabView" TabBarBackground="HotPink" TabWidthMode="SizeToContent" >
<tabView:SfTabView.Items >
<tabView:SfTabItem Header="Call" FontSize="18">
<tabView:SfTabItem.Content>
<ListView RowHeight="50">
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>James</x:String>
<x:String>Richard</x:String>
<x:String>Michael</x:String>
<x:String>Alex</x:String>
<x:String>Clara</x:String>
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="10,5">
<Label VerticalOptions="Start"
HorizontalOptions="Start"
TextColor="#666666"
FontSize="16"
Text="{Binding}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</tabView:SfTabItem.Content>
</tabView:SfTabItem>
<tabView:SfTabItem Header="Favorite" FontSize="18" >
<tabView:SfTabItem.Content>
<ScrollView >
<StackLayout Spacing="20" >
<ListView RowHeight="50">
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Steve</x:String>
<x:String>Mark</x:String>
<x:String>Alan</x:String>
<x:String>Sandra</x:String>
<x:String>Ryan</x:String>
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="10,5">
<Label VerticalOptions="Start"
HorizontalOptions="Start"
TextColor="#666666"
FontSize="16"
Text="{Binding}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</StackLayout>
</ScrollView>
</tabView:SfTabItem.Content>
</tabView:SfTabItem>
<tabView:SfTabItem Header="Contacts" FontSize="18" >
<tabView:SfTabItem.Content>
<ListView RowHeight="50">
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Sandra</x:String>
<x:String>Ryan</x:String>
<x:String>Michael</x:String>
<x:String>Mark</x:String>
<x:String>Clara</x:String>
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="10,5">
<Label VerticalOptions="Start"
HorizontalOptions="Start"
TextColor="#666666"
FontSize="16"
Text="{Binding}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</tabView:SfTabItem.Content>
</tabView:SfTabItem>
</tabView:SfTabView.Items>
</tabView:SfTabView> </StackLayout>
</ContentPage>
Expand Down
61 changes: 61 additions & 0 deletions TabViewSample/TabViewSample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#if WINDOWS
using Microsoft.UI.Windowing;
#elif MACCATALYST
using UIKit;
#endif

namespace TabViewSample
{
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
}

}

27 changes: 27 additions & 0 deletions TabViewSample/TabViewSample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Syncfusion.Maui.Core.Hosting;
using Microsoft.Extensions.Logging;

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

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

return builder.Build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
11 changes: 11 additions & 0 deletions TabViewSample/TabViewSample/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace TabViewSample
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
}
Loading

0 comments on commit df13c81

Please sign in to comment.