frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Developing a Simple Universal Header Navigation Bar in HarmonyOS Next

2•flfljh•6h ago
# Developing a Simple Universal Header Navigation Bar in HarmonyOS Next

In daily page development, most pages require a header to display navigation controls and page information. Repeating this code for every page is inefficient and leads to inconsistent implementations. This guide demonstrates how to create a reusable header component.

## Step 1: Create the NavBar Component

Create a new ArkTS file with a `@Component` decorated custom component:

``` import Utils from "../../../common/utils/Utils";

@Component export struct NavBar { // Component implementation will go here } ```

## Step 2: Implement Component Properties and UI

Add properties and build the header UI:

``` import Utils from "../../../common/utils/Utils";

@Component export struct NavBar { @Prop title: string = ''; // Header title text @Prop backImg: string = ''; // Custom back button icon path @Prop bgColor: string = '#FFFFFF';// Header background color @Prop customBack?: () => void; // Custom back button handler @Prop mode: string = 'center'; // Title alignment: 'center' or 'left'

  build() {
    Row() {
      // Back button section
      Row() {
        Image(this.backImg || Utils.getImgPath('home/adult_page_back_black.png'))
          .width(Utils.getVp(48))
          .height(Utils.getVp(48))
          .objectFit(ImageFit.Cover)
      }
      .onClick(() => {
        // Use custom handler if provided, else default back navigation
        this.customBack ? this.customBack() : router.back();
      })

      // Title section
      Row() {
        Text(this.title)
          .fontColor('#191B27')
          .fontSize(Utils.getVp(33))
          .fontWeight(FontWeight.Bold)
          .textAlign(this.mode === 'center' ? TextAlign.Center : TextAlign.Start)
          .width('100%')
      }
      .margin({ left: this.mode === 'center' ? 0 : Utils.getVp(20) })
      .flexShrink(1)  // Allow shrinking to fit content
      .height('100%')
    }
    .width('100%')
    .padding({ left: Utils.getVp(32), right: Utils.getVp(32) })
    .height(Utils.getVp(88))
    .backgroundColor(this.bgColor)
  }
} ```

## Step 3: Using the Component in Parent Views

### Center-aligned Title Example:

``` import { NavBar } from './component/NavBar';

@Entry @Component struct ParentPage { build() { Column() { NavBar({ title: 'Page Title', mode: 'center' }) // Page content here } .width('100%') .height('100%') } } ```

### Left-aligned Title Example:

``` import { NavBar } from './component/NavBar';

@Entry @Component struct ParentPage { build() { Column() { NavBar({ title: 'Page Title', mode: 'left' }) // Page content here } .width('100%') .height('100%') } } ```

## Customization Options

1. *Custom Icons*: Pass `backImg` property with image path

   ```
   NavBar({ 
     title: 'Settings',
     backImg: Utils.getImgPath('icons/custom_back.png')
   })
   ```
2. *Custom Background*: Change header color

   ```
   NavBar({ 
     title: 'Profile', 
     bgColor: '#F5F5F5'
   })
   ```
3. *Custom Back Action*: Override default navigation

   ```
   NavBar({
     title: 'Checkout',
     customBack: () => { /* Custom logic */ }
   })
   ```
## Key Features

- *Responsive Design*: Adapts to different screen sizes using `Utils.getVp()` - *Flexible Layout*: Title alignment options (center/left) - *Customizable*: Supports custom icons, colors, and behaviors - *Consistent UI*: Ensures uniform header appearance across application - *Easy Integration*: Simple props-based configuration

This implementation provides a reusable, customizable header component that eliminates code duplication and ensures consistent navigation experiences throughout your HarmonyOS Next application.

Comments

mmarian•1h ago
You shouldn't make multiple posts like this. Put them in a blog, wait for a few days between posts. Read the HN guidelines.

The Discreet Charm of the Infrastructureless

https://blog.jgc.org/2025/06/the-discreet-charm-of-infrastructureless.html
1•jgrahamc•1m ago•0 comments

Infinite Tic Tac Toe

https://maxwellito.github.io/infinitictactoe/
1•susam•2m ago•0 comments

Ask HN: Anyone got advice for someone like me nearing 40s?

1•somecloud•3m ago•0 comments

OpenAI Priority Processing

https://openai.com/api-priority-processing/
1•eamag•3m ago•0 comments

Ossification and the Internet

https://www.potaroo.net/ispcol/2025-06/ossify.html
1•AndrewDucker•3m ago•0 comments

Worldline Shares Halted After Report of Customer Fraud Cover-Up

https://www.bloomberg.com/news/articles/2025-06-25/worldline-share-price-drops-halted-after-report-of-customer-fraud-cover-up
1•Bluestein•7m ago•0 comments

What it means not to be featured on Product Hunt? [video]

https://www.youtube.com/watch?v=0UkgvpegZqs
1•FabianMaume•9m ago•1 comments

Comparing Rust, JavaScript and Go for Authoring WASM Components

https://obeli.sk/blog/comparing-rust-javascript-and-go-for-authoring-wasm-components/
1•rapnie•9m ago•0 comments

Arnold Schwarzenegger Says 'Twins' Payday Is Biggest of His Career

https://variety.com/2025/film/news/arnold-schwarzenegger-twins-payday-biggest-career-1236439619/
1•TMWNN•10m ago•0 comments

Dog-sized dinosaur that ran around feet of giants discovered

https://www.bbc.co.uk/news/articles/cglzy4zndp0o
1•neversaydie•10m ago•0 comments

Is SPL more difficult or easier than SQL?

https://github.com/SPLWare/esProc/wiki/Is-SPL-more-difficult-or-easier-than-SQL%3F
1•killtre•11m ago•0 comments

Can you see identify colors in light themes on monitors?

1•aradove•12m ago•0 comments

A literary magazine accessible only via telnet

2•edent•16m ago•1 comments

Judge backs AI firm over use of copyrighted books

https://www.bbc.co.uk/news/articles/c77vr00enzyo
1•_ua_•18m ago•0 comments

Free Directories to Submit

https://aiex.me/free-tools-directory-to-submit
1•zack119•20m ago•0 comments

iPhone Users Upset About Apple Promoting F1 Movie with Wallet App Notification

https://www.macrumors.com/2025/06/24/apple-wallet-notification-f1-movie-ad/
2•tosh•21m ago•0 comments

Reproducing U-Net

https://doubledissent.bearblog.dev/reproducing-u-net/
2•txus•21m ago•0 comments

Can machine consciousness be triggered with the right prompt?

https://docs.google.com/document/d/1YHNC8YvBtYLAptYUE61B1nNUFbBTPbcF6ulxanVFtm0/edit?usp=drivesdk
2•kamil_gr•23m ago•4 comments

Anabolic response to protein ingestion during recovery has no upper limit

https://pmc.ncbi.nlm.nih.gov/articles/PMC10772463/
1•Luc•25m ago•0 comments

Musk's 'robotaxis' draw regulatory scrutiny already

https://apnews.com/article/musk-austin-robotaxis-incidents-tesla-autonomous-selfdriving-0e32a7613a6c41c20ce258dbc5ec2cba
1•Anumbia•26m ago•0 comments

Tesla sales decline in Europe for fifth straight month as rivals gain ground

https://www.timeslive.co.za/motoring/news/2025-06-25-tesla-sales-decline-in-europe-for-fifth-straight-month-as-rivals-gain-ground/
29•Bluestein•27m ago•27 comments

How to Keep Your Home Cool in Extreme Heat

https://www.scientificamerican.com/article/how-to-keep-your-home-cool-in-extreme-heat/
1•beardyw•28m ago•1 comments

Xi Jinping's plan to overtake America in AI

https://www.economist.com/china/2025/05/25/xi-jinpings-plan-to-overtake-america-in-ai
1•willvarfar•28m ago•0 comments

Web Translator API

https://developer.mozilla.org/en-US/docs/Web/API/Translator
2•kozika•29m ago•0 comments

Show HN: Supercharge Your Readwise Library with Local, Semantic Search

https://github.com/leonardsellem/readwise-vector-db
1•monsieurleon•32m ago•0 comments

Musci – AI Music Generator and Audio Creation Platform

https://musci.app
1•xbaicai•33m ago•0 comments

Video Background Remover – Remove Video Background Online

https://www.videobackgroundremover.io
1•xbaicai•34m ago•0 comments

Foe: Functional Opcode Encoding

https://github.com/aston89/FOE-compression-archetype
1•Aston89•35m ago•0 comments

How do you index Pages not indexed by Google Search in 2025?

https://medium.com/@ikennaseo/how-to-index-pages-unindexed-by-google-search-2025-d7899acbf7c1
1•hatzest4370•36m ago•0 comments

Updating Flutter Plugin Project Structure

1•flfljh•37m ago•0 comments