The navigation component supports setting the stackBuilder and bottomBuilder attributes for the title bar since version 6.0.0(20).
When you need to add custom nodes to the title bar, such as inserting segmented buttons in the upper title bar region, or adding search boxes and tabs in the lower title bar area, you can leverage the title bar custom area configuration capability. The height of the title bar is typically controlled by the system or framework. When adding a custom node, you need to ensure that it does not exceed the available space of the title bar. Otherwise, layout overflow or visual clutter may occur. The custom area may overlap or affect the default title bar components (such as the back button and title text). Exercise caution when designing the layout to avoid interaction conflicts or blocking key elements. Adding a large number of components with complex interactions and high rendering frequency to the title bar may affect performance.


Import required modules.
// Starting from version 6.0.2(22), you do not need to manually import HdsNavigationAttribute. For details, refer to the "Modules to Import" section of the HdsNavigation reference document.
import { HdsNavigation, HdsNavigationTitleMode, HdsNavigationAttribute } from '@kit.UIDesignKit';
import { ItemRestriction, SegmentButton, SegmentButtonOptions, SegmentButtonTextItem } from '@kit.ArkUI'; Create a level-1 navigation component. Then set the stackBuilder and bottomBuilder attributes of the content attribute in titleBar to configure the custom area of the navigation component.
@Entry
@Component
struct Index {
@Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack();
scroller: Scroller = new Scroller();
@State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({
buttons: [{ text: 'Notes' }, { text: 'To-dos' }] as ItemRestriction<SegmentButtonTextItem>,
selectedFontColor: '#ffe6ba0b',
selectedBackgroundColor: Color.White,
textPadding: {
top: 5,
right: 5,
bottom: 5,
left: 5
}
});
@State tabSelectedIndexes: number[] = [0];
@Builder
stackBuilder() {
Row() {
Flex({ justifyContent: FlexAlign.SpaceBetween }) {
Button() {
SymbolGlyph($r('sys.symbol.open_sidebar'))
.fontColor([$r('sys.color.icon_primary')])
.fontSize(24)
.width(24)
.height(24)
}
.width(40)
.height(40)
.backgroundColor($r('sys.color.ohos_id_color_button_normal'))
SegmentButton({
options: this.tabOptions,
selectedIndexes: $tabSelectedIndexes
})
.width(150)
Button() {
SymbolGlyph($r('sys.symbol.dot_grid_2x2'))
.fontColor([$r('sys.color.icon_primary')])
.fontSize(24)
.width(24)
.height(24)
}
.backgroundColor($r('sys.color.ohos_id_color_button_normal'))
.width(40)
.height(40)
}
.margin({ left: 16, right: 16 })
}
.width('100%')
}
build() {
HdsNavigation(this.pageInfos) { // Create the **HdsNavigation** component.
Row() {
Text('All Notes')
.fontSize(26)
.fontWeight(FontWeight.Bold)
.layoutWeight(1)
.onClick(() => {
this.pageInfos.pushPath({ name: 'pageOne' });
})
}
.margin({ left: 16, top: 16 })
.justifyContent(FlexAlign.Start)
}
.titleBar({
enableComponentSafeArea: true, // Set the title bar as a component-level safe area so that the content area can avoid the title bar.
content: {
title: { mainTitle: '' },
// Set the custom title area for HdsNavigation.
stackBuilder: (): void => this.stackBuilder(),
}
})
.hideBackButton(true)
.bindToScrollable([this.scroller]) // Bind the navigation component to the scrollable container component.
.titleMode(HdsNavigationTitleMode.MINI)
}
} Create a level-2 navigation component on the PageOne page. Call the titleBar API to adapt the title bar of the HdsNavDestination component to the HarmonyOS style and configure the title bar content. The following uses NavPathStack routing as an example.
// PageOne.ets
// Import required modules.
// Starting from version 6.0.2(22), you do not need to manually import HdsNavDestinationAttribute. For details, refer to the "Modules to Import" section of the HdsNavDestination reference document.
import { BottomBuilderShowType, HdsNavDestination, HdsNavDestinationAttribute } from '@kit.UIDesignKit';
@Builder
export function PageOneBuilder() {
PageOne()
}
@Component
export struct PageOne {
@Consume('pageInfos') pageInfos: NavPathStack;
scroller: Scroller = new Scroller();
@Builder
bottomBuilder() {
Column() {
Search({ placeholder: 'Search' })
.height(40)
.placeholderColor($r('sys.color.font_primary'))
.margin({ left: 16, right: 16 })
}
.width('100%')
}
build() {
HdsNavDestination() { // Create the **HdsNavDestination** component.
Scroll(this.scroller) { // Set a scrollable container component in the content area to implement a dynamic title bar blur effect that synchronizes with content scrolling.
Image($r('app.media.scenery2')) // scenery2 is a custom resource. You need to replace it with your local resource.
.height('100%')
}
.edgeEffect(EdgeEffect.Spring)
.scrollBar(BarState.Off)
.margin({ left: 16, right: 16 })
.clip(false) // Do not clip the area of child components that extend beyond the current component's bounds, so that the content area can be displayed below the title bar.
}
.titleBar({
enableComponentSafeArea: true, // Set the title bar as a component-level safe area so that the content area can avoid the title bar.
content: {
// Set the title bar content for the **HdsNavigation** component.
title: {
// Set the title of the title bar for the **HdsNavigation** component.
mainTitle: 'PageOne',
},
// Define the back button settings of the title bar of the **HdsNavigation** component.
backIcon: {
label: 'backIcon', // Accessibility text
componentId: 'backIconId', // ID of the back button
},
// Set the custom area below the title bar, including the height and display type.
bottomBuilder: {
builder: (): void => this.bottomBuilder(),
height: 56,
showType: BottomBuilderShowType.DIRECTLY_SHOW
},
menu: {
// Set the menu items for the title bar of the **HdsNavigation** component.
value: [{
// Set the menu item content.
content: {
label: 'menu',
icon: $r('sys.symbol.ohos_circle'),
},
}]
},
}
})
.bindToScrollable([this.scroller]) // Bind the navigation component to the scrollable container component.
}
} Add configurations to the module selection of the entry/src/main/module.json5 configuration file of the project for page navigation.
"routerMap": "$profile:route_map"
Add the route_map.json file to the entry/src/main/resources/base/profile directory of the project.
{
"routerMap": [
{
"name": "pageOne",
"pageSourceFile": "src/main/ets/pages/PageOne.ets",
"buildFunction": "PageOneBuilder",
"data": {
"description": "this is pageOne"
}
}
]
}