{"id":48526,"date":"2021-02-24T10:05:58","date_gmt":"2021-02-24T18:05:58","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=48526"},"modified":"2021-02-23T11:42:59","modified_gmt":"2021-02-23T19:42:59","slug":"xamarin-community-toolkit-tabview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/xamarin-community-toolkit-tabview\/","title":{"rendered":"Beautiful Tabs with Xamarin Community Toolkit TabView"},"content":{"rendered":"<p>One of the hardest parts of apps to customize is the tab bar. To overcome this, we&#8217;ve added the <code>TabView<\/code> to the <a href=\"https:\/\/aka.ms\/xct\" rel=\"noopener\" target=\"_blank\">Xamarin Community Toolkit<\/a>. With the <code>TabView<\/code> you have full control over the look-and-feel of the tab bar. In this blogpost we&#8217;ll learn what it&#8217;s all about.<\/p>\n<h2>Xamarin.Forms TabbedPage<\/h2>\n<p>The way to implement a tab bar in Xamarin.Forms is by using a <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/app-fundamentals\/navigation\/tabbed-page\" rel=\"noopener\" target=\"_blank\">TabbedPage<\/a>. This works great and maps to the native equivalents on all platforms supported, but that is just the thing: it uses the native look and feel. Over the years we&#8217;ve surfaced more APIs that allow you to style the tab bar more, but the native platforms kept limiting us. There was a big ask for having a bit action button floating on that tab bar or being able to scroll through tabs, have animations, and more!<\/p>\n<p>To be able to provide you with all the above, and more, there is now&#8230; <code>TabView<\/code>!<\/p>\n<h2>TabView: All You&#8217;ve Ever Wanted from a Tab Bar<\/h2>\n<p>By creating a tab bar from all Xamarin.Forms elements a whole new world opens. We&#8217;re able to bind items to tabs, scroll through them (both the pages as the actual tabs!), have animations, badges, and most of all&#8230; We can implement that big, floating, rounded action button. The <code>TabView<\/code> is available in the Xamarin Community Toolkit, today! But before you go off and install it right away, let me show you a couple of the cool things it can do.<\/p>\n<h3>Basic Usage<\/h3>\n<p>Basic usage of the <code>TabView<\/code> looks like below. You can already see so many options that are not available on the default tab bar. Placement top or bottom, background color, specify a height, enable an indicator <em>and<\/em> give it a color. It&#8217;s crazy.<\/p>\n<p>If we look a little bit closer, we see that we have a TabView at the root and inside of the you&#8217;ll need to use <code>TabViewItem<\/code>. Those items even have more properties to customize. But more importantly; inside of these items you will specify the content. So, in practice, the <code>TabViewItem<\/code>s will act as your actual tabs.<\/p>\n<pre><code class=\"xml\">\n&lt;Grid&gt;\n        &lt;xct:TabView\n                TabStripPlacement=\"Bottom\"\n                TabStripBackgroundColor=\"Blue\"\n                TabStripHeight=\"60\"\n                TabIndicatorColor=\"Yellow\"\n                TabContentBackgroundColor=\"Yellow\"&gt;\n\n                &lt;xct:TabViewItem\n                    Icon=\"triangle.png\"\n                    Text=\"Tab 1\"\n                    TextColor=\"White\"\n                    TextColorSelected=\"Yellow\"\n                    FontSize=\"12\"&gt;\n                    &lt;Grid \n                        BackgroundColor=\"Gray\"&gt;\n                        &lt;Label\n                            HorizontalOptions=\"Center\"\n                            VerticalOptions=\"Center\"\n                            Text=\"TabContent1\" \/&gt;\n                    &lt;\/Grid&gt;\n                &lt;\/xct:TabViewItem&gt;\n\n                &lt;xct:TabViewItem\n                    Icon=\"circle.png\"\n                    Text=\"Tab 2\"\n                    TextColor=\"White\"\n                    TextColorSelected=\"Yellow\"\n                    FontSize=\"12\"&gt;\n                    &lt;Grid&gt;\n                        &lt;Label    \n                            HorizontalOptions=\"Center\"\n                            VerticalOptions=\"Center\"\n                            Text=\"TabContent2\" \/&gt;\n                    &lt;\/Grid&gt;\n                &lt;\/xct:TabViewItem&gt;\n        &lt;\/xct:TabView&gt;\n  &lt;\/Grid&gt;\n<\/code><\/pre>\n<p>When running the above code, it will show up like in the screenshot below.<\/p>\n<p><center>\n  <a href=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/BasicTabView.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/BasicTabView-1024x274.png\" alt=\"Basic TabView Implementation Running on iOS\" width=\"640\" height=\"171\" class=\"alignnone size-large wp-image-48543\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/BasicTabView-1024x274.png 1024w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/BasicTabView-300x80.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/BasicTabView-768x205.png 768w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/BasicTabView.png 1170w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a>\n<\/center><\/p>\n<p>See this full sample code in context (or run it!) in the <a href=\"https:\/\/github.com\/xamarin\/XamarinCommunityToolkit\/tree\/main\/samples\/XCT.Sample\" rel=\"noopener\" target=\"_blank\">Xamarin Community Toolkit sample app<\/a>.<\/p>\n<h3>TabView With Action Button<\/h3>\n<p>I&#8217;ve already mentioned it: the floating action button that looks so fancy in all those designs. You can see an example in the screenshot below.<\/p>\n<p><center>\n  <a href=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/TabViewActionButton.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/TabViewActionButton.png\" alt=\"TabView with Action Button Running on Android\" width=\"421\" height=\"93\" class=\"alignnone size-full wp-image-48547\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/TabViewActionButton.png 421w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2021\/02\/TabViewActionButton-300x66.png 300w\" sizes=\"(max-width: 421px) 100vw, 421px\" \/><\/a>\n<\/center><\/p>\n<p>To achieve this, we can use the following code. This is again taken from <a href=\"https:\/\/github.com\/xamarin\/XamarinCommunityToolkit\/blob\/main\/samples\/XCT.Sample\/Pages\/Views\/TabView\/CustomTabsPage.xaml\" rel=\"noopener\" target=\"_blank\">an example<\/a> in the Xamarin Community Toolkit sample app. The code has been adapted to show just the crucial things here.<\/p>\n<pre><code class=\"xml\">\n        &lt;xct:TabView\n                TabStripPlacement=\"Bottom\"\n                TabStripBackgroundColor=\"Blue\"\n                TabStripHeight=\"60\"\n                TabIndicatorColor=\"Yellow\"\n                TabContentBackgroundColor=\"Yellow\"&gt;\n\n                &lt;!-- First TabViewItem --&gt;\n\n                &lt;xct:TabViewItem\n                    Text=\"Tab 2\"   \n                    Icon=\"circle.png\"\n                    ControlTemplate=\"{StaticResource FabTabItemTemplate}\"\n                    Style=\"{StaticResource TabItemStyle}\"\n                    TabTapped=\"OnFabTabTapped\" \/&gt;\n\n                &lt;!-- Third TabViewItem --&gt;\n        &lt;\/xct:TabView&gt;\n<\/code><\/pre>\n<p>The thing you might notice about this code is that the middle action button is just another <code>TabViewItem<\/code>. But by not assigning any content and instead assigning an event to the <code>TabTapped<\/code> property, you suddenly gain a button instead of another tab. Another thing that stands out is that you can use a <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/app-fundamentals\/templates\/control-template\" rel=\"noopener\" target=\"_blank\"><code>ControlTemplate<\/code><\/a> to style the <code>TabViewItem<\/code> any way you want. I&#8217;ll admit that the tabs shown in this post might need a bit more styling&#8230;<\/p>\n<h3>What About Lazy Loading?<\/h3>\n<p>That is a question we hear a lot, and with good reason! Lazy loading is not built into the TabView directly, however, also in the Xamarin Community Toolkit, we have the <a href=\"https:\/\/github.com\/xamarin\/XamarinCommunityToolkit\/pull\/796\" rel=\"noopener\" target=\"_blank\">LazyView<\/a>. With LazyView you can make <em>any<\/em> view lazy loading, including the ones used in TabView. LazyView isn&#8217;t out in stable yet at the time of writing this blog but will be part of version 1.1 and available in the nightly pre-release versions.<\/p>\n<h2>And Much, Much More&#8230;<\/h2>\n<p>I wish I could go over all the great stuff that is in the <code>TabView<\/code>, but where&#8217;s the fun in that? I invite you to go give it a try yourself and let us know. If you want to learn more, make sure to go check out <a href=\"https:\/\/docs.microsoft.com\/xamarin\/community-toolkit\/views\/tabview\" rel=\"noopener\" target=\"_blank\">the Docs page<\/a>, the <a href=\"https:\/\/github.com\/xamarin\/XamarinCommunityToolkit\/tree\/main\/samples\" rel=\"noopener\" target=\"_blank\">sample app<\/a> that we have for the Xamarin Community Toolkit or the video down below where I talk with Javier about all the things that you can do with this.<\/p>\n<p>I can&#8217;t wait to see what beautiful designs you can produce!<\/p>\n<h2>See it in Action!<\/h2>\n<p>If you want to see the TabView in action, right now, check out the video of the Xamarin Show below. In this episode I have Javier as my guest who shows everything that is possible. <center>\n  <iframe title=\"TabView (Xamarin Community Toolkit)\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/B0uyt9HhotY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the hardest parts of apps to customize is the tab bar. To overcome this, we&#8217;ve added the TabView to the Xamarin Community Toolkit. With the TabView you have full control over the look-and-feel of the tab bar. In this blogpost we&#8217;ll learn what it&#8217;s all about.<\/p>\n","protected":false},"author":13350,"featured_media":48554,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,367],"tags":[9196,9195,27,9173],"class_list":["post-48526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-xamarin-forms","tag-customize-tab-bar","tag-tabview","tag-xamarin","tag-xamarin-community-toolkit"],"acf":[],"blog_post_summary":"<p>One of the hardest parts of apps to customize is the tab bar. To overcome this, we&#8217;ve added the TabView to the Xamarin Community Toolkit. With the TabView you have full control over the look-and-feel of the tab bar. In this blogpost we&#8217;ll learn what it&#8217;s all about.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/48526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/13350"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=48526"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/48526\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/48554"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=48526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=48526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=48526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}