Getting Started
此内容尚不支持你的语言。
-
Install the package
Terminal window npm i @capgo/capacitor-navigation-barTerminal window pnpm add @capgo/capacitor-navigation-barTerminal window yarn add @capgo/capacitor-navigation-barTerminal window bun add @capgo/capacitor-navigation-bar -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync -
Configure the plugin
Set Navigation Bar Color:
import { NavigationBar } from '@capgo/capacitor-navigation-bar';// Set navigation bar colorawait NavigationBar.setColor({color: '#FF0000', // Red colordarkButtons: false // Use light buttons});Dynamic Theme Support:
// Set color based on themeconst isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;await NavigationBar.setColor({color: isDarkMode ? '#000000' : '#FFFFFF',darkButtons: !isDarkMode});Minimum Requirements:
- Android 5.0 (API level 21) or higher
- The plugin only works on Android devices
No additional setup required in AndroidManifest.xml.
This plugin is Android-only. iOS does not have a customizable navigation bar.
-
Advanced usage
import { NavigationBar } from '@capgo/capacitor-navigation-bar';export class ThemeService {// Set transparent navigation barasync setTransparent() {await NavigationBar.setColor({color: '#00000000', // TransparentdarkButtons: true});}// Match app themeasync matchAppTheme(primaryColor: string, isLight: boolean) {await NavigationBar.setColor({color: primaryColor,darkButtons: isLight});}// Get current navigation bar colorasync getCurrentColor() {const result = await NavigationBar.getColor();console.log('Current color:', result.color);return result.color;}// Reset to defaultasync resetToDefault() {await NavigationBar.setColor({color: '#000000',darkButtons: false});}} -
Integration with app lifecycle
import { App } from '@capacitor/app';import { NavigationBar } from '@capgo/capacitor-navigation-bar';// Update navigation bar on app state changesApp.addListener('appStateChange', async ({ isActive }) => {if (isActive) {// Restore your app's navigation bar colorawait NavigationBar.setColor({color: '#YourAppColor',darkButtons: true});}});// Handle theme changeswindow.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', async (e) => {await NavigationBar.setColor({color: e.matches ? '#121212' : '#FFFFFF',darkButtons: !e.matches});});
API Reference
Methods
setColor(options: ColorOptions)
Set the navigation bar color and button style.
Parameters:
options
: Configuration objectcolor
: string - Hex color code (e.g., ‘#FF0000’)darkButtons
: boolean - Use dark buttons (true) or light buttons (false)
Returns: Promise<void>
getColor()
Get the current navigation bar color.
Returns: Promise<{ color: string }>
Interfaces
interface ColorOptions { color: string; // Hex color code darkButtons: boolean; // Button style}
interface ColorResult { color: string; // Current hex color}
Platform Notes
Android
- Requires Android 5.0 (API level 21) or higher
- Color changes are immediate
- Transparent colors are supported (use alpha channel)
- The navigation bar might not be visible on devices with gesture navigation
iOS
- This plugin has no effect on iOS devices
- iOS does not provide APIs to customize the system navigation
Common Use Cases
- Brand Consistency: Match navigation bar with your app’s primary color
- Theme Support: Adapt to light/dark themes dynamically
- Immersive Experiences: Use transparent navigation for full-screen content
- Status Indication: Change color to indicate app states (recording, errors, etc.)
Best Practices
-
Color Contrast: Ensure sufficient contrast between navigation bar and buttons
// Good contrast examplessetColor({ color: '#FFFFFF', darkButtons: true }); // White bar, dark buttonssetColor({ color: '#000000', darkButtons: false }); // Black bar, light buttons -
Theme Consistency: Update navigation bar when theme changes
-
Accessibility: Consider users with visual impairments when choosing colors
-
Performance: Avoid frequent color changes that might distract users
Troubleshooting
Navigation bar not changing:
- Verify the device is running Android 5.0+
- Check if the device has gesture navigation enabled
- Ensure color format is valid hex (e.g., ‘#FF0000’)
Buttons not visible:
- Check the
darkButtons
parameter matches your background color - Light backgrounds need
darkButtons: true
- Dark backgrounds need
darkButtons: false
Color appears different:
- Some Android versions may modify the color slightly
- Use fully opaque colors for best results