在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。