在React Native中設計navigationBar通常有兩種方式:
import { createStackNavigator } from 'react-navigation';
import Screen1 from './Screen1';
import Screen2 from './Screen2';
const AppNavigator = createStackNavigator({
Screen1: {
screen: Screen1,
navigationOptions: {
title: 'Screen 1',
},
},
Screen2: {
screen: Screen2,
navigationOptions: {
title: 'Screen 2',
},
},
});
export default createAppContainer(AppNavigator);
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const CustomNavBar = ({ title }) => (
<View style={styles.navBar}>
<Text style={styles.title}>{title}</Text>
</View>
);
const styles = StyleSheet.create({
navBar: {
height: 60,
backgroundColor: 'lightblue',
justifyContent: 'center',
alignItems: 'center',
},
title: {
fontSize: 20,
color: 'white',
},
});
export default CustomNavBar;
然后在各個屏幕中引入并使用CustomNavBar組件:
import CustomNavBar from './CustomNavBar';
const Screen1 = () => (
<View>
<CustomNavBar title="Screen 1" />
<Text>Screen 1 content</Text>
</View>
);
export default Screen1;
無論是使用React Navigation庫還是自定義導航欄組件,都可以根據項目需求來選擇適合的方式來設計和實現navigationBar。