Xamarin.forms: [Spec] TabView

Created on 19 May 2020  Â·  24Comments  Â·  Source: xamarin/Xamarin.Forms

TabView

We can have tabs using Shell. However, what happens if we want to have nested tabs within a specific section (Example: Grid)?, _what if we want to fully customize each tab?_. In these cases, we would need a Custom Renderer so far...

The TabView is a way to display a set of tabs and their respective content. TabView is useful for displaying several content while giving a user the capability to customize mostly everything.

tabview

NOTE: TabView is a cross-platform view that takes over when native tabs hit their limits, such as positioning with layouts, styling, and non-uniform styling like a raised button.

API

Next, a list with the TabView properties, events and visualstates.

Properties

TabView Properties

| Property | Type | Description |
|----------|:-------------:|:-------------:|
| TabItemsSource | IEnumerable | A collection used to generate the TabView's tab items. |
| TabViewItemDataTemplate | DataTemplate | the template the Tab View uses to generate tab items' header. |
| TabContentDataTemplate | DataTemplate | The template the Tab View uses to generate tab items' content. |
| IsCyclical | Bool | Enable or disable cyclical tabs navigation. |
| IsLazy | Bool | Enable or disable lazy tabs loading. |
| SelectedIndex | Int | Gets or sets the currently selected tab. Default is 0. |
| TabStripPlacement | TabStripPlacement | The TabStrip placement (top or bottom). |
| TabStripBackground | Brush | The TabStrip background. |
| TabIndicatorBrush | Brush | The TabIndicator background. |
| TabIndicatorHeight | double | The TabIndicator height. |
| TabIndicatorWidth | double | The TabIndicator width. |
| TabIndicatorPlacement | TabIndicatorPlacement | |
| TabIndicatorView | View | The TabIndicator content. |
| TabContentBackground | Brush | The tab content background. |
| TabContentHeight | Double | The tab content height. |
| TabStripHeight | Double | The TabStrip height. |
| TabContentHeight | Double | The tab content height. |
| HasTabStripShadow | Bool | Show or hide the TabStrip shadow effect. |
| IsTabTransitionEnabled | Bool | Enable or disable the transition between tabs. |
| IsSwipeEnabled | Bool | Enable or disable the swipe gesture. |

TabViewItem Properties

| Property | Type | Description |
|----------|:-------------:|:-------------:|
| Text | String | The text of the tab. |
| TextColor | Color | The text color of the tab. |
| TextColorSelected | Color | The text color of the selected tab. |
| FontSize | FontSize | The font size used in the tab text. |
| FontSizeSelected | FontSize | The font size used in the selected tab. |
| FontFamily | String | The font family used in the tab. |
| FontFamilySelected | String | The font family used in the selected tab. |
| FontAttributes | FontAttributes | The font attributes used in the tab. |
| FontAttributesSelected | FontAttributes | The font attributes used in the selected tab. |
| Icon | ImageSource | The icon of the tab. |
| IconSelected | ImageSource | The ImageSource used as icon in the selected tab. |
| Content | View | The content of the tab. Is View, can use anything as content. |
| BadgeText | Bool | The badge text used in the tab. |
| BadgeTextColor | Color | The badge text color used in the tab. |
| BadgeTextColorSelected | Color | The badge text color used in the selected tab. |
| BadgeBackgroundColor | Color | The badge color used in the tab. |
| BadgeBackgroundColorSelected | Color | The badge color used in the selected tab. |
| IsSelected | Bool | a bool that indicate if the tab is selected or not. |
| TapCommand | ICommand | Command that is executed when the user tap a tab. |
| TapCommandParameter | object | The tap command parameter. |

Events

TabView Events

| Event | Description |
|----------|:-------------:|
| SelectionChanged | Event that is raised when the selected tab changed. |
| Scrolled | Event that is raised when is swiping between tabs. |

TabViewItem Events

| Event | Description |
|----------|:-------------:|
| TabTapped | Event that is raised when the user tap a tab. |

VisualStates

The Visual State Manager (VSM) provides a structured way to make visual changes to the user interface from code.
The VSM introduces the concept of visual states. TabView can have several different visual appearances depending on its underlying state.

TabView have four specific VisualStates:

  • CurrentTabVisualState
  • NextTabVisualState
  • PreviousTabVisualState
  • DefaultTabVisualState

Scenarios

Let's see some samples covering common scenarios.

Basic Tabs

Let's see a basic example:

<TabView 
    TabStripPlacement="Bottom"
    TabStripBackgroundColor="Blue">
    <TabViewItem
        Icon="triangle.png"
        Text="Tab 1">
        <Grid 
            BackgroundColor="Gray">
            <Label
                HorizontalOptions="Center"
                VerticalOptions="Center"
                Text="TabContent1" />
        </Grid>
    </TabViewItem>
    <TabViewItem
        Icon="circle.png"
        Text="Tab 2">
        <Grid>
            <Label    
                HorizontalOptions="Center"
                VerticalOptions="Center"
                Text="TabContent2" />
        </Grid>
    </TabViewItem>
</TabView>

basic-tabs

TabItemsSource

Using TabItemsSource (dynamic tabs):

<TabView
    TabItemsSource="{Binding Monkeys}"
    TabViewItemDataTemplate="{StaticResource TabViewItemTemplate}"
    TabContentDataTemplate="{StaticResource TabContentTemplate}" />

tabitemssource

Custom Tabs

The appearance of each tab can be customized:

<ControlTemplate
    x:Key="TabItemTemplate">
    <Grid>
    ...
    </Grid>
</ControlTemplate>

<TabView>
    <TabViewItem
        Text="Tab 1"
        ControlTemplate="{StaticResource TabItemTemplate}">
    </TabViewItem>
</TabView>

custom-tabs

Cyclical Tabs

Do you want to navigate between the tabs cyclically?

<TabView
    IsCyclical="True">
    ...
</TabView>

iscyclical

Lazy Loading

Lazy tab loading:

<TabView
    IsLazy="True">
    ...
</TabView>

lazy-tabs

Tab Transitions and TabViewItem animations

Can use Xamarin.Forms animations to customize the transition between each tab, animate the tab when appears or disappears, or even animate the badge when appears or disappears.

<TabView>
    <TabView.TabTransition>
        <local:CustomTabTransition />
    </TabView.TabTransition>
    <TabViewItem
        Text="Tab 1">      
            <TabViewItem.TabAnimation>
                <local:CustomTabViewItemAnimation />
            </TabViewItem.TabAnimation>
        <Grid 
            BackgroundColor="LawnGreen">
            <Label
                HorizontalOptions="Center"
                VerticalOptions="Center"
                Text="TabContent1" />
        </Grid>
    </TabViewItem>
    ...
</TabView>

custom-tabs-animation

Using VisualStates

Can use different visual states to customize the current tab, the next tab, etc.

<Grid>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="TabViewStates">
            <VisualState x:Name="CurrentTab">
                <VisualState.Setters>
                    <Setter Property="Opacity" Value="1" />
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="PreviousTab">
                <VisualState.Setters>
                    <Setter Property="Opacity" Value="0.7" />
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="NextTab">
                <VisualState.Setters>
                    <Setter Property="Opacity" Value="0.7" />
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="DefaultTab">
                <VisualState.Setters>
                    <Setter Property="Opacity" Value="0.9" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Label Text="{Binding Index}" HorizontalOptions="Center" VerticalOptions="End" />
</Grid>

Create Tabs using C

You can use XAML or C# to create the UI in Xamarin.Forms. Let's see how we would create tabs using C#.

var tabView = new Tabs
{
    TabStripPlacement = TabStripPlacement.Bottom,
    TabStripBackgroundColor = Color.Blue,
    TabStripHeight = 60,
    TabIndicatorColor = Color.Yellow,
    TabContentBackgroundColor = Color.Yellow
};

var tabViewItem1 = new TabViewItem
{
    Icon = "triangle.png",
    Text = "Tab 1",
    TextColor = Color.White,
    TextColorSelected = Color.Yellow,
};

var tabViewItem1Content = new Grid
{
    BackgroundColor = Color.Gray
};

var label1 = new Label
{
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center,
    Text = "TabContent1"
};

tabViewItem1Content.Children.Add(label1);

tabViewItem1.Content = tabViewItem1Content;

tabView.TabItems.Add(tabViewItem1);

var tabViewItem2 = new TabViewItem
{
    Icon = "circle.png",
    Text = "Tab 2",
    TextColor = Color.White,
    TextColorSelected = Color.Yellow
};

var tabViewItem2Content = new Grid
{
    BackgroundColor = Color.OrangeRed
};

var label2 = new Label
{
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center,
    Text = "TabContent2"
};

tabViewItem2Content.Children.Add(label2);

tabViewItem2.Content = tabViewItem2Content;

tabView.TabItems.Add(tabViewItem2);

Using Styles

Can customize the appearance of the tab content, tab strip, tab item, etc. using XAML styles or CSS.

Using XAML:

<Style
    x:Key="TabItemStyle"
    TargetType="TabViewItem">
    <Setter
        Property="FontSize"
        Value="12" />
    <Setter
        Property="TextColor"
        Value="#979797" />
</Style>

Using CSS:

.customTabItem {
    font-size: medium;
    text-color: red;
}

Difficulty : Medium

high impact proposal-open roadmap enhancement âž•

Most helpful comment

Awesome control. However, I think this can be updated a little to cover a design like this:

ezgif com-optimize

Properties

TabView Properties (Additional)

Property | Type | Description
--|--|--
CornerRadius | float | Allows to set the radius of a the corners from the tab view
HeightRequest | double | Allows to set the height of the control
WidthRequest | double | Allows to set the width of the control

TabViewItem Properties (Additional)

Property | Type | Description
--|--|--
IconPlacement | IconPlacement | Allows to set the location of the icon on the tab: Top, Bottom, Left, Right
CornerRadius | float | Allows to set the radius of a the corners from the tab view
BadgeIcon | ImageSource | The image to be used as a badge indicator. Due to something we just need to show a small dot on it without text or numbers.

All 24 comments

wow!

Beautiful Stuff

Good job!

Regarding the name "TabView", I am concerned that it will create some confusion with the one in UWP, especially when the day comes where that control is mapped to Xamarin.Forms/MAUI.

And I do hope that day comes since it would be a nice control to have for UWP, iPad and Android Tablet. It would be even cooler if it had a way to seamlessly transform for phones as well.

Awesome control. However, I think this can be updated a little to cover a design like this:

ezgif com-optimize

Properties

TabView Properties (Additional)

Property | Type | Description
--|--|--
CornerRadius | float | Allows to set the radius of a the corners from the tab view
HeightRequest | double | Allows to set the height of the control
WidthRequest | double | Allows to set the width of the control

TabViewItem Properties (Additional)

Property | Type | Description
--|--|--
IconPlacement | IconPlacement | Allows to set the location of the icon on the tab: Top, Bottom, Left, Right
CornerRadius | float | Allows to set the radius of a the corners from the tab view
BadgeIcon | ImageSource | The image to be used as a badge indicator. Due to something we just need to show a small dot on it without text or numbers.

This looks great!

@vhugogarcia Thanks for your feedback, very interesting!.

With the Spec definition, you could already manage the size of the TabView, even the content or the TabStrip height. About have CornerRadius, mnnn, I suppose it would be interesting if we have the option in the TabStrip, right?. Something like TabStripCornerRadius.

In the case of the TabViewItem, you can use a template to customize the content. So you can manage Corner Radius in the TabViewItem. However, we can have the CornerRadius property and allow to customize the corners also in the default templates.

Then we have the IconPlacement and BadgeIcon. Both seem like good ideas to me.

I will update the Spec.

Can I use this without Shell?

It's just a bunch of forms elements put together

you can use it wherever you can stick a view :-)

Hi guys, fantastic stuff can you make sure these cool controls work without shell? Thousand of apps do not use shell and we use Prism which as you know cannot use shell till shell allows proper integration with dependency injection. thank you

@developer9969 Please see the previous two comments and the answer of PureWeen

@developer9969 Yes, you can use it without shell too.

Nice ,I have been looking forward to it for a long time.Thank sir!

Awesome!!, when can it be used?

When I am viewing my application on an iPad in landscape, I have wide margins but I would also like to have a wide margin for the tab icons so that for example the icons are evenly spaced but they have a 200 margin to the right and to the left. Will this be possible?

@rscholey Yes, will be possible.

@jsuarezruiz I saw that TabView has been "Cut" from the roadmap. Does this mean that it won't resurface until MAUI ? (Same question for AppBar)

Hi @timahrentlov, yes we cut it from the upcoming Forms releases in order to make additional room for the foundational improvements we need to focus on.

In order to still ship these controls sooner we are planning to move them to the XamarinCommunityToolkit. We have good momentum there ( _insert contributor recruiting pitch ;)_ ) and I hope this will be a reasonable option during this transitional period.

@jsuarezruiz please close this issue here and move it to .NET MAUI. Same for the AppBar.

@davidortinau can we expect to see updated roadmap soon? Even if it's just "under the hood" changes, I'm sure many would be interested to see what is the team actually working on.

Oh what a bummer. :(
I was really desperately waiting for this one.

I haven't seen a possibility to register routes for TabViewItems. Would it be possible to register routes for TabViewItems and navigate through them by using absolute navigation?

Was it already moved to https://github.com/xamarin/XamarinCommunityToolkit? I can't find it there. Or does no code exist yet?

Was it already moved to https://github.com/xamarin/XamarinCommunityToolkit? I can't find it there. Or does no code exist yet?

https://github.com/jsuarezruiz/Xamarin.Forms.TabView I think this is the one

@AswinPG this looks good but i think that it is older than this issue and there is not nuget for it. Still can be used for anyone needs urgently i believe

@jsuarezruiz your repo looks already fine. you never released a nuget package for this?

Was this page helpful?
0 / 5 - 0 ratings