• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Tabs
2
3
4When there is a large amount of page information, to enable the user to focus on the currently displayed content, the page content needs to be classified to improve the page space utilization. The [\<Tabs>](../reference/arkui-ts/ts-container-tabs.md) component can quickly switch between views on a page, improving information search efficiency and reducing the amount of information that users can obtain at a time.
5
6
7## Basic Layout
8
9  The **\<Tabs>** component consists of two parts: **\<TabContent>** and **\<TabBar>**. **\<TabContent>** is the content page, and **\<TabBar>** is the navigation tab bar. The following figure shows the page structure. The layout varies according to the navigation type. In bottom navigation, top navigation, and side navigation, the navigation tab bar is located at the bottom, top, and edge, respectively.
10  **Figure 1** \<Tabs> component layout
11
12![tabs-layout](figures/tabs-layout.png)
13
14
15>**NOTE**
16>
17> - The **\<TabContent>** component does not support setting of the common width attribute. By default, its width is the same as that of the parent **\<Tabs>** component.
18>
19> - The **\<TabContent>** component does not support setting of the common height attribute. Its height is determined by the height of the parent **\<Tabs>** component and the **\<TabBar>** component.
20
21
22**\<Tabs>** use braces to enclose the tab content, as shown in Figure 2.
23
24
25  **Figure 2** Using \<Tabs> and \<TabContent>
26
27![tabs-tabscontent](figures/tabs-tabscontent.png)
28
29
30Each **\<TabContent>** component should be mapped to a tab page, which can be configured through the **tabBar** attribute. The following is an example.
31
32```ts
33 TabContent() {
34   Text('Content of the Home tab').fontSize(30)
35 }
36.tabBar ('Home')
37```
38
39
40When setting multiple **\<TabContent>** components, place them in sequence in the **\<Tabs>** component.
41
42```ts
43Tabs() {
44  TabContent() {
45    Text('Content of the Home tab').fontSize(30)
46  }
47  .tabBar ('Home')
48
49  TabContent() {
50    Text('Content of the Recommended tab').fontSize(30)
51  }
52  .tabBar ('Recommended')
53
54  TabContent() {
55    Text ('Content of the Discover tab').fontSize (30)
56  }
57  .tabBar ('Discover')
58
59  TabContent() {
60    Text ('Content of the Me tab').fontSize (30)
61  }
62  .tabBar ("Me")
63}
64```
65
66
67## Bottom Navigation
68
69Bottom navigation is the most common navigation mode in applications. The bottom navigation bar is located at the bottom of the level-1 page of the application. It enables the user to quickly have a picture of the feature categories the moment they open the application. In addition, it facilitates one-hand operations of the user. Bottom navigation generally exists as a main navigation form of an application, in that it provides convenient access to primary destinations anywhere in the application.
70
71
72  **Figure 3** Bottom navigation bar
73
74![bottom-navigation](figures/bottom-navigation.gif)
75
76
77You set the position of the navigation bar through the **barPosition** parameter of the **\<Tabs>** component. By default, **barPosition** is set to **BarPosition.Start**, which means that the navigation bar is located on the top. To display the navigation bar at the bottom, set **barPosition** to **BarPosition.End**.
78
79
80```ts
81Tabs({ barPosition: BarPosition.End }) {
82  // TabContent: Home, Discover, Recommended, and Me
83  ...
84}
85```
86
87
88## Top Navigation
89
90Top navigation comes in handy when there are many content categories and users need to frequently switch between them. It is usually a further subdivision of the categories in the bottom navigation bar. For example, a theme application may provide a top navigation bar that classifies themes into image, video, and font.
91
92  **Figure 4** Top navigation bar
93
94![top-navigation](figures/top-navigation.gif)
95
96
97```ts
98Tabs({ barPosition: BarPosition.Start }) {
99  // TabContent: Following, Video, Game, Digital, Technology, Sports, Movie
100  ...
101}
102```
103
104
105## Side Navigation
106
107Side navigation is seldom used in applications. It is more applicable to landscape screens. Because the natural eye movement pattern is from left to right, the side navigation bar is located on the left side by default.
108
109
110  **Figure 5** Side navigation bar
111
112![side-navigation](figures/side-navigation.png)
113
114
115To implement the side navigation bar, set the **vertical** attribute of the **\<Tabs>** component to **true**. By default, **vertical** is set to **false**, indicating that the content page and navigation bar are aligned vertically.
116
117
118
119```ts
120Tabs({ barPosition: BarPosition.Start }) {
121  // TabContent: Home, Discover, Recommended, and Me
122  ...
123}
124.vertical(true)
125.barWidth(100)
126.barHeight(200)
127```
128
129
130>**NOTE**
131>
132> - When the **vertical** attribute is set to **false**, the tab bar takes up the whole screen width by default. Set **barWidth** to a proper value.
133>
134> - When the **vertical** attribute is set to **true**, the tab bar takes up the actual content height by default. Set **barWidth** to a proper value.
135
136
137## Restricting the Scrolling of the Navigation Bar
138
139  By default, the navigation bar is scrollable. On some pages that require multi-level classification of content, for example, when both bottom navigation and top navigation are used, the scroll effect of the bottom navigation bar may conflict with that of the top navigation bar. In this case, the scrolling of the bottom navigation bar needs to be restricted to improve user experience.
140  **Figure 6** Restricting the scrolling of the bottom navigation bar
141
142![restricted-navigation](figures/restricted-navigation.gif)
143
144
145The attribute that enables or disables the scrolling is **scrollable**. Its default value is **true**, indicating that scrolling is enabled. To disable the scrolling, set the attribute to **false**.
146
147```ts
148Tabs({ barPosition: BarPosition.End }) {
149  TabContent(){
150    Column(){
151      Tabs(){
152        // Content on the top navigation bar
153        ...
154      }
155    }
156    .backgroundColor('#ff08a8f1')
157    .width('100%')
158  }
159  .tabBar ('Home')
160
161  // Other TabContent content: Discover, Recommended, and Me
162  ...
163}
164.scrollable(false)
165```
166
167
168## Fixed Navigation Bar
169
170When the content categories are relatively fixed and not scalable, a fixed navigation bar can be used. For example, it can be used for the bottom navigation bar, which generally contains 3 to 5 categories. The fixed navigation bar cannot be scrolled or dragged. The tab bar width is evenly distributed among the categories.
171
172
173  **Figure 7** Fixed navigation bar
174
175![fixed-navigation](figures/fixed-navigation.gif)
176
177
178To use a fixed navigation bar, set the **barMode** attribute of the **\<Tabs>** component to **barMode.Fixed** (default).
179
180```ts
181Tabs({ barPosition: BarPosition.End }) {
182  // TabContent: Home, Discover, Recommended, and Me
183  ...
184}
185.barMode(BarMode.Fixed)
186```
187
188
189## Scrollable Navigation Bar
190
191The top navigation bar or side navigation bar can be set to be scrollable if the screen width cannot fully accommodate all the tabs. With a scrollable navigation bar, users can reveal tabs beyond the visible area by touching or swiping on the navigation bar.
192
193
194  **Figure 8** Scrollable navigation bar
195
196![scrollable-navigation](figures/scrollable-navigation.gif)
197
198
199To use a scrollable navigation bar, set the **barMode** attribute of the **\<Tabs>** component to **BarMode.Scrollable**.
200
201```ts
202Tabs({ barPosition: BarPosition.Start }) {
203  // TabContent: follow, video, game, digital, technology, sports, movie, humanities, art, nature, and military
204  ...
205}
206.barMode(BarMode.Scrollable)
207```
208
209
210## Customizing the Navigation Bar
211
212The bottom navigation bar is generally used on the home page of an application. To deliver a more vibrant experience, you can customize the style of the navigation bar, combining use of text and icons to signify the tab content.
213
214
215  **Figure 9** Custom navigation bar
216
217![custom-navigation-bar](figures/custom-navigation-bar.png)
218
219
220By default, the system uses an underscore (_) to indicate the active tab. For a custom navigation bar, you need to implement the corresponding style to distinguish active tabs from inactive tabs.
221
222
223To customize the navigation bar, use the **tabBar** parameter and pass in to it custom function component styles in **CustomBuilder** mode. In this example, a custom function component **tabBuilder** is declared, and the input parameters include **title** (tab title), **targetIndex** (target index of the tab), **selectedImg** (image for the selected state), and **normalImg** (image for the unselected state). The UI display style is determined based on whether the value of **currentIndex** (index of the active tab) matches that of **targetIndex** (target index of the tab).
224
225```ts
226@Builder tabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
227  Column() {
228    Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
229      .size({ width: 25, height: 25 })
230    Text(title)
231      .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
232  }
233  .width('100%')
234  .height(50)
235  .justifyContent(FlexAlign.Center)
236}
237```
238
239
240Pass the custom function component to the **tabBar** attribute corresponding to the tab content and transfer the corresponding parameters.
241
242```ts
243TabContent() {
244  Column(){
245    Text('Content of the Me tab')
246  }
247  .width('100%')
248  .height('100%')
249  .backgroundColor('#007DFF')
250}
251.tabBar(this.tabBuilder('Me', 0, $r('app.media.mine_selected'), $r('app.media.mine_normal')))
252```
253
254
255## Switching to a Specified Tab
256
257Non-custom navigation bars follow the default system switching logic. If you are using a custom navigation bar, you must manually implement the logic for switching tabs so that when the user switches to a tab, the application displays the corresponding tab page.
258
259
260  **Figure 10** Switching to a specified tab in a custom navigation bar
261
262![switching-to-a-specified-tab](figures/switching-to-a-specified-tab.gif)
263
264
265To switch to a specified tab page, use **TabsController**, which is the controller of the **\<Tabs>** component. By using the **changeIndex** API of **TabsController**, you can set your application to display the tab content corresponding to the specified index.
266
267```ts
268class Tmp{
269  currentIndex:number = 0;
270  tabsController : TabsController = new TabsController()
271  foo(val:number){
272    this.currentIndex = val;
273  }
274  tabFoo(){
275    this.tabsController.changeIndex(this.currentIndex);
276  }
277}
278private tabsController : TabsController = new TabsController()
279@State currentIndex:number = 0;
280
281@Builder tabBuilder(title: string, targetIndex: number) {
282  Column() {
283    Text(title)
284      .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
285  }
286  ...
287  .onClick(() => {
288    let cur:Tmp = new Tmp()
289    cur.foo(targetIndex)
290    cur.tabFoo()
291  })
292}
293```
294
295
296When using a custom navigation bar, pass the corresponding \@Builder in the **tabBar** attribute and transfer the corresponding parameters.
297
298```ts
299Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) {
300  TabContent(){
301    ...
302  }.tabBar(this.tabBuilder('Home',0))
303
304  TabContent(){
305    ...
306  }.tabBar (this.tabBuilder ('Discover', 1))
307
308  TabContent(){
309    ...
310  }.tabBar (this.tabBuilder ('Recommended', 2))
311
312  TabContent(){
313    ...
314  }
315  .tabBar(this.tabBuilder('Me', 3))
316}
317```
318
319
320## Swiping to Switch Between Tabs
321
322For non-custom navigation bars, tabs and tab content are linked by default. For custom navigation bars, however, tabs and tab content are linked when tab switching is initiated by **TabsController**, but not when tab switching is initiated by a swipe gesture. This means that, when the user swipes on the screen to switch between tab content, the tabs do not switch automatically. In this case, manual switching is required.
323
324
325  **Figure 11** Lack of linkage between tabs and tab content
326
327![lack-of-linkage](figures/lack-of-linkage.gif)
328
329
330To manually switch between the tabs, use the **onChange** API provided by the **\<Tabs>** component to listen for the index change and pass the index of the active tab to **currentIndex**.
331
332
333```ts
334class Tmp{
335  currentIndex:number = 0;
336  foo(val:number){
337    this.currentIndex = val;
338  }
339}
340Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) {
341  TabContent() {
342    ...
343  }.tabBar(this.tabBuilder('Home', 0))
344
345  TabContent() {
346    ...
347  }.tabBar(this.tabBuilder('Discover', 1))
348
349  TabContent() {
350    ...
351  }.tabBar(this.tabBuilder('Recommended', 2))
352
353  TabContent() {
354    ...
355  }
356  .tabBar(this.tabBuilder('Me', 3))
357}.onChange((index:number) => {
358  let cur:Tmp = new Tmp()
359  cur.foo(index)
360})
361```
362
363
364  **Figure 12** Linkage between tabs and tab content
365
366![final-effect](figures/final-effect.gif)
367
368