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 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 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 33 34```ts 35 TabContent() { 36 Text('Content of the Home tab').fontSize(30) 37 } 38.tabBar ('Home') 39``` 40 41 42When setting multiple **\<TabContent>** components, place them in sequence in the **\<Tabs>** component. 43 44 45 46```ts 47Tabs() { 48 TabContent() { 49 Text('Content of the Home tab').fontSize(30) 50 } 51 .tabBar ('Home') 52 53 TabContent() { 54 Text('Content of the Recommended tab').fontSize(30) 55 } 56 .tabBar ('Recommended') 57 58 TabContent() { 59 Text ('Content of the Discover tab').fontSize (30) 60 } 61 .tabBar ('Discover') 62 63 TabContent() { 64 Text ('Content of the Me tab').fontSize (30) 65 } 66 .tabBar ("Me") 67} 68``` 69 70 71## Bottom Navigation 72 73Bottom 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. 74 75 76 **Figure 3** Bottom navigation bar 77 78 79 80 81You 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**. 82 83 84```ts 85Tabs({ barPosition: BarPosition.End }) { 86 // TabContent: Home, Discover, Recommended, and Me 87 ... 88} 89``` 90 91 92## Top Navigation 93 94Top 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. 95 96 **Figure 4** Top navigation bar 97 98 99 100 101```ts 102Tabs({ barPosition: BarPosition.Start }) { 103 // TabContent: Following, Video, Game, Digital, Technology, Sports, Movie 104 ... 105} 106``` 107 108 109## Side Navigation 110 111Side 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. 112 113 114 **Figure 5** Side navigation bar 115 116 117 118 119To 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. 120 121 122 123```ts 124Tabs({ barPosition: BarPosition.Start }) { 125 // TabContent: Home, Discover, Recommended, and Me 126 ... 127} 128.vertical(true) 129.barWidth(100) 130.barHeight(200) 131``` 132 133 134>**NOTE** 135> 136> - When the **vertical** attribute is set to **true**, the tab bar takes up the whole screen width by default. You need to set **barWidth** to a proper value. 137> 138> - When the **vertical** attribute is set to **true**, the tab bar takes up the actual content height by default. You need to set **barWidth** to a proper value. 139 140 141## Restricting the Scrolling of the Navigation Bar 142 143 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. 144 **Figure 6** Restricting the scrolling of the bottom navigation bar 145 146 147 148 149The 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**. 150 151 152 153```ts 154Tabs({ barPosition: BarPosition.End }) { 155 TabContent(){ 156 Column(){ 157 Tabs(){ 158 // Content on the top navigation bar 159 ... 160 } 161 } 162 .backgroundColor('#ff08a8f1') 163 .width('100%') 164 } 165 .tabBar ('Home') 166 167 // Other TabContent content: Discover, Recommended, and Me 168 ... 169} 170.scrollable(false) 171``` 172 173 174## Fixed Navigation Bar 175 176When 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. 177 178 179 **Figure 7** Fixed navigation bar 180 181 182 183 184To use a fixed navigation bar, set the **barMode** attribute of the **\<Tabs>** component to **barMode.Fixed** (default). 185 186 187 188```ts 189Tabs({ barPosition: BarPosition.End }) { 190 // TabContent: Home, Discover, Recommended, and Me 191 ... 192} 193.barMode(BarMode.Fixed) 194``` 195 196 197## Scrollable Navigation Bar 198 199The 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. 200 201 202 **Figure 8** Scrollable navigation bar 203 204 205 206 207To use a scrollable navigation bar, set the **barMode** attribute of the **\<Tabs>** component to **BarMode.Scrollable**. 208 209 210 211```ts 212Tabs({ barPosition: BarPosition.Start }) { 213 // TabContent: follow, video, game, digital, technology, sports, movie, humanities, art, nature, and military 214 ... 215} 216.barMode(BarMode.Scrollable) 217``` 218 219 220## Customizing the Navigation Bar 221 222The 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. 223 224 225 **Figure 9** Custom navigation bar 226 227 228 229 230By 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. 231 232 233To 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). 234 235 236 237```ts 238@Builder TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) { 239 Column() { 240 Image(this.currentIndex === targetIndex ? selectedImg : normalImg) 241 .size({ width: 25, height: 25 }) 242 Text(title) 243 .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B') 244 } 245 .width('100%') 246 .height(50) 247 .justifyContent(FlexAlign.Center) 248} 249``` 250 251 252Pass the custom function component to the **tabBar** attribute corresponding to the tab content and transfer the corresponding parameters. 253 254 255 256```ts 257TabContent() { 258 Column(){ 259 Text('Content of the Me tab') 260 } 261 .width('100%') 262 .height('100%') 263 .backgroundColor('#007DFF') 264} 265.tabBar(this.TabBuilder('Me', 0, $r('app.media.mine_selected'), $r('app.media.mine_normal'))) 266``` 267 268 269## Switching to a Specified Tab 270 271Non-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. 272 273 274 **Figure 10** Switching to a specified tab in a custom navigation bar 275 276 277 278 279To 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. 280 281 282 283```ts 284class Tmp{ 285 currentIndex:number = 0; 286 tabsController : TabsController = new TabsController() 287 foo(val:number){ 288 this.currentIndex = val; 289 } 290 tabfoo(){ 291 this.tabsController.changeIndex(this.currentIndex); 292 } 293} 294private tabsController : object = new TabsController() 295@State currentIndex:number = 0; 296 297@Builder TabBuilder(title: string, targetIndex: number) { 298 Column() { 299 Text(title) 300 .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B') 301 } 302 ... 303 .onClick(() => { 304 let Cur:Tmp = new Tmp() 305 Cur.foo(targetIndex) 306 Cur.tabfoo() 307 }) 308} 309``` 310 311 312When using a custom navigation bar, pass the corresponding \@Builder in the **tabBar** attribute and transfer the corresponding parameters. 313 314 315 316```ts 317Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) { 318 TabContent(){ 319 ... 320 }.tabBar (this.TabBuilder ('Home', 0)) 321 322 TabContent(){ 323 ... 324 }.tabBar (this.TabBuilder ('Discover', 1)) 325 326 TabContent(){ 327 ... 328 }.tabBar (this.TabBuilder ('Recommended', 2)) 329 330 TabContent(){ 331 ... 332 } 333 .tabBar (this.TabBuilder ('Me',3)) 334} 335``` 336 337 338## Swiping to Switch Between Tabs 339 340For 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. 341 342 343 **Figure 11** Lack of linkage between tabs and tab content 344 345 346 347 348To 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**. 349 350 351 352 353```ts 354class Tmp{ 355 currentIndex:number = 0; 356 foo(val:number){ 357 this.currentIndex = val; 358 } 359} 360Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) { 361 TabContent() { 362 ... 363 }.tabBar (this.TabBuilder ('Home', 0)) 364 365 TabContent() { 366 ... 367 }.tabBar (this.TabBuilder ('Found', 1)) 368 369 TabContent() { 370 ... 371 }.tabBar (this.TabBuilder ('Recommended', 2)) 372 373 TabContent() { 374 ... 375 } 376 .tabBar (this.TabBuilder ('Me', 3)) 377}.onChange((index:number) => { 378 let Cur:Tmp = new Tmp() 379 Cur.foo(index) 380}) 381``` 382 383 384 **Figure 12** Linkage between tabs and tab content 385 386 387 388