TabView
<TabView>
is a navigation component that shows content grouped into tabs and lets users switch between tabs.
Examples
Example: Simple TabView
Using a <TabView>
inside an Angular
app requires some special attention about how to provide title, iconSource and content (view) of the TabViewItem
.
In a pure NativeScript application TabView has an items property which could be set via XML to an array of <TabViewItem>
s (basically, an array of objects with title, view and iconSource properties).
However, NativeScript-Angular does not support nested properties in its HTML template, so adding <TabViewItem>
to <TabView
> is a little different.
NativeScript-Angular provides a custom Angular directive that simplifies the way native <TabView>
should be used.
The following example shows how to add a <TabView>
to your page (with some clarifications later):
<TabView selectedIndex="0" (selectedIndexChanged)="onSelectedIndexchanged($event)">
<StackLayout *tabItem="{title: 'First Tab', iconSource: 'res://icon'}">
<StackLayout>
<label
text="First Tab"
textWrap="true"
class="m-15 h2 text-left"
color="blue"
></label>
</StackLayout>
</StackLayout>
<StackLayout *tabItem="{title: 'Second Tab', iconSource: 'res://icon'}">
<StackLayout>
<label
text="Second Tab"
textWrap="true"
class="m-15 h2 text-left"
color="blue"
></label>
</StackLayout>
</StackLayout>
</TabView>
If you have set the iconSource property on a |
<TabView
loaded="onLoaded"
selectedIndex="{{tabSelectedIndex}}"
selectedIndexChanged="onSelectedIndexChanged"
androidTabsPosition="bottom"
androidOffscreenTabLimit="0"
>
<TabViewItem title="Profile">
<StackLayout>
<Label
text="{{ tabSelectedIndexResult }}"
class="h2 m-t-16 text-center"
textWrap="true"
/>
<Button text="Change Tab" tap="changeTab" class="btn btn-primary btn-active" />
</StackLayout>
</TabViewItem>
<TabViewItem title="Stats">
<StackLayout>
<Label
text="{{ tabSelectedIndexResult }}"
class="h2 m-t-16 text-center"
textWrap="true"
/>
<Button text="Change Tab" tap="changeTab" class="btn btn-primary btn-active" />
</StackLayout>
</TabViewItem>
<TabViewItem title="Settings">
<StackLayout>
<Label
text="{{ tabSelectedIndexResult }}"
class="h2 m-t-16 text-center"
textWrap="true"
/>
<Button text="Change Tab" tap="changeTab" class="btn btn-primary btn-active" />
</StackLayout>
</TabViewItem>
</TabView>
import {
Dialogs,
Observable,
TabView,
SelectedIndexChangedEventData
} from '@nativescript/core'
export function onLoaded(args) {
const tabView = args.object as TabView
const vm = new Observable()
vm.set('tabSelectedIndex', 0)
vm.set('tabSelectedIndexResult', 'Profile Tab (tabSelectedIndex = 0 )')
tabView.bindingContext = vm
}
export function changeTab(args) {
const vm = args.object.bindingContext
const tabSelectedIndex = vm.get('tabSelectedIndex')
if (tabSelectedIndex === 0) {
vm.set('tabSelectedIndex', 1)
} else if (tabSelectedIndex === 1) {
vm.set('tabSelectedIndex', 2)
} else if (tabSelectedIndex === 2) {
vm.set('tabSelectedIndex', 0)
}
}
// displaying the old and new TabView selectedIndex
export function onSelectedIndexChanged(args: SelectedIndexChangedEventData) {
if (args.oldIndex !== -1) {
const newIndex = args.newIndex
const vm = (args.object as TabView).bindingContext
if (newIndex === 0) {
vm.set('tabSelectedIndexResult', 'Profile Tab (tabSelectedIndex = 0 )')
} else if (newIndex === 1) {
vm.set('tabSelectedIndexResult', 'Stats Tab (tabSelectedIndex = 1 )')
} else if (newIndex === 2) {
vm.set('tabSelectedIndexResult', 'Settings Tab (tabSelectedIndex = 2 )')
}
Dialogs.alert(
`Selected index has changed ( Old index: ${args.oldIndex} New index: ${args.newIndex} )`
).then(() => {
console.log('Dialog closed!')
})
}
}
import { SelectedIndexChangedEventData } from '@nativescript/core'
;<tabView
selectedIndex={selectedIndex}
onSelectedIndexChange={(args: SelectedIndexChangedEventData) => {
const { oldIndex, newIndex } = args
console.log(`Changed from tab index ${oldIndex} -> ${newIndex}.`)
}}
>
<tabViewItem nodeRole="items" title="Tab 1">
<label text="Content for Tab 1" />
</tabViewItem>
<tabViewItem nodeRole="items" title="Tab 2">
<label text="Content for Tab 2" />
</tabViewItem>
</tabView>
<tabView selectedIndex="{selectedIndex}" on:selectedIndexChange="{indexChange}">
<tabViewItem title="Tab 1">
<label text="Content for Tab 1" />
</tabViewItem>
<tabViewItem title="Tab 2">
<label text="Content for Tab 2" />
</tabViewItem>
</tabView>
function indexChange(event) {
let newIndex = event.value
console.log('Current tab index: ' + newIndex)
}
<TabView :selectedIndex="selectedIndex" @selectedIndexChange="indexChange">
<TabViewItem title="Tab 1">
<label text="Content for Tab 1" />
</TabViewItem>
<TabViewItem title="Tab 2">
<label text="Content for Tab 2" />
</TabViewItem>
</TabView>
methods: {
indexChange: function(args) {
let newIndex = args.value
console.log('Current tab index: ' + newIndex)
}
}
Currently, |
Example: Adding icons to tabs
<TabView :selectedIndex="selectedIndex" iosIconRenderingMode="alwaysOriginal">
<TabViewItem title="Tab 1" iconSource="~/images/icon.png">
<label text="Content for Tab 1" />
</TabViewItem>
<TabViewItem title="Tab 2" iconSource="~/images/icon.png">
<label text="Content for Tab 2" />
</TabViewItem>
</TabView>
<tabView selectedIndex="{selectedIndex}" iosIconRenderingMode="alwaysOriginal">
<tabViewItem title="Tab 1" iconSource="~/images/icon.png">
<label text="Content for Tab 1" />
</tabViewItem>
<tabViewItem title="Tab 2" iconSource="~/images/icon.png">
<label text="Content for Tab 2" />
</tabViewItem>
</tabView>
<tabView selectedIndex={selectedIndex} iosIconRenderingMode="alwaysOriginal">
<tabViewItem nodeRole="items" title="Tab 1" iconSource="~/images/icon.png">
<label text="Content for Tab 1" />
</tabViewItem>
<tabViewItem nodeRole="items" title="Tab 2" iconSource="~/images/icon.png">
<label text="Content for Tab 2" />
</tabViewItem>
</tabView>
You can use images for tab icons instead of icon fonts. For more information about how to control the size of icons, see Working with image from resource folders. |
Styling
The TabView
component has the following unique styling properties:
-
tabTextColor
(corresponding CSS propertytab-text-color
) - Changes the text color for the tabs. -
selectedTabTextColor
(corresponding CSS propertyselected-tab-text-color
) - Changes the color of the text for the selected tab. -
tabBackgroundColor
(corresponding CSS propertytab-background-color
) - Sets the background color of the tabs. -
tabTextFontSize
(corresponding CSS propertytab-text-font-size
) - Sets the font size of the tabs. -
textTransform
(corresponding CSS propertytext-transform
) - Sets the text transform individually for everyTabViewItem
. Value options:capitalize
,lowercase
,none
, anduppercase
. -
androidSelectedTabHighlightColor
^android specific property^ (corresponding CSS propertyandroid-selected-tab-highlight-color
) - Sets the underline color of the tabs in Android.
Props
Name | Type | Description |
---|---|---|
|
|
Gets or sets the currently selected tab.
Default is |
|
|
(Style property) Gets or sets the text color of the tab titles. |
|
|
Gets or sets the font size of the tab titles. |
|
|
(Style property) Gets or sets the background color of the tabs. |
|
|
(Style property) Gets or sets the text color of the selected tab title. |
|
|
Sets the position of the TabView in Android platform + Valid values: |
|
|
Gets or sets the number of tabs that should be retained to either side of the current tab in the view hierarchy in an idle state. |
|
|
Gets or sets the color of the horizontal line drawn below the currently selected tab on Android. |
|
|
Gets or sets the icon rendering mode on iOS. |
|
|
Additional inherited properties not shown. Refer to the API Reference |
Events
Name | Description |
---|---|
|
Emits an event object containing an |