Preview 2.0 is now in Public Beta!
Read the Announcement
Note: You are on the beta version of our docs. This is a work in progress and may contain broken links and pages.

<SegmentedBar> is a UI bar component that displays a set of buttons for discrete selection. Can show text or images.

As opposed to <TabView>:

  • The position of <SegmentedBar> is not fixed.
  • You can place and style it as needed on the page or inside additional app elements such as hamburger menus.
  • You need to handle the content shown after selection separately.

Creating a SegmentedBar with SegmentedBarItem

xml
<SegmentedBar>
  <SegmentedBarItem title="First" />
  <SegmentedBarItem title="Second" />
  <SegmentedBarItem title="Third" />
</SegmentedBar>

SegmentedBar with selectedIndex

To handle a selectedIndex change, listen to the selectedIndexChanged event.

xml
<SegmentedBar row="0" class="m-5" selectedIndex="{{ sbSelectedIndex }}"
 selectedIndexChanged=" {{ onSelectedIndexChange }} ">
  <SegmentedBar.items>
    <SegmentedBarItem title="Item 1" />
    <SegmentedBarItem title="Item 2" />
    <SegmentedBarItem title="Item 3" />
  </SegmentedBar.items>
</SegmentedBar>
ts
import { Observable } from '@nativescript/core'
export class HelloWorldModel extends Observable {
  sbSelectedIndex = 0

  onSelectedIndexChange(args: SelectedIndexChangedEventData) {
    console.log('new value: ' + args.newIndex, 'old: ' + args.oldIndex)
  }
}

Props

items

ts
const segmentedBarItem1 = new SegmentedBarItem()
segmentedBarItem1.title = 'Item 1'

const segmentedBarItem2 = new SegmentedBarItem()
segmentedBarItem2.title = 'Item 2'

//SegmentedBar
const segmentedBar = new SegmentedBar()

segmentedBar.items = [segmentedBarItem1, segmentedBarItem2]

An array of items to be displayed in the segmented bar. Represents the button labels or icons of the segmented bar.


selectedIndex

xml
<SegmentedBar selectedIndex="{{ sbSelectedIndex }}">
ts
export class HelloWorldModel extends Observable {
  sbSelectedIndex = 0
}

Gets or sets the index of the selected item.


selectedBackgroundColor

xml
<SegmentedBar selectedBackgroundColor="red">
</SegmentedBar>

Gets or sets the background color of the selected item. To set the background color of the entire bar, use backgroundColor.


...Inherited

For additional inherited properties, refer to the API Reference.

Events

selectedIndexChanged

xml
<SegmentedBar selectedIndexChanged=" {{ onSelectedIndexChange }} ">
ts
onSelectedIndexChange(args: SelectedIndexChangedEventData) {
    const segmentedBar = args.object as SegmentedBar
}

Emitted when the an item on the segmented bar is tapped. See the SelectedIndexChangedEventData interface for the event data.


SelectedIndexChangedEventData interface

The SelectedIndexChangedEventData object provides the following data:

NameTypeDescription
oldIndexnumberThe old selected index.
newIndexnumberThe new selected index.

Native component

AndroidiOS
android.widget.TabHostUISegmentedControl
Previous
SearchBar
Next
Slider