Header 抽屉导航 react-navigation

Header for DrawerNavigation with react-navigation

我在使用 ReactNative,我正在使用 native-base 和 react-navigation npm。

我明白了,我的问题是我如何才能有一个 header,直到按钮 "Home",我查看了 react-navigation 的文档,但它并没有真正被清除。

https://github.com/react-community/react-navigation/blob/master/docs/api/navigators/DrawerNavigator.md

像这样(图片是固定的,只是在这里放个标志)

您可以为抽屉实现自定义内容组件。在那里,您还可以使用 DrawerItems 简单地呈现导航项。例如:

import React from 'react'
import { Text, View } from 'react-native'
import { DrawerItems, DrawerNavigation } from 'react-navigation'

const DrawerContent = (props) => (
  <View>
    <View
      style={{
        backgroundColor: '#f50057',
        height: 140,
        alignItems: 'center',
        justifyContent: 'center',
      }}
    >
      <Text style={{ color: 'white', fontSize: 30 }}>
        Header
      </Text>
    </View>
    <DrawerItems {...props} />
  </View>
)

const Navigation = DrawerNavigator({
  // ... your screens
}, {
  // define customComponent here
  contentComponent: DrawerContent,
})

对于抽屉导航,您可以添加您自己的 Header 和页脚,并使用 contentComponent 配置您自己的样式:
首先import { DrawerItems, DrawerNavigation } from 'react-navigation'然后

Header 之前 DrawerItems:

contentComponent: props => <ScrollView><Text>Your Own Header Area Before</Text><DrawerItems {...props} /></ScrollView> .

DrawerItems 之后的页脚:

contentComponent: props => <ScrollView><DrawerItems {...props} /><Text>Your Own Footer Area After</Text></ScrollView> .

您可以使用抽屉导航配置中的 contentComponent 选项来实现它。根据所需的配置级别,您可以采用两种方法:

方法一

DrawerItems 来自 react-navigation(自行处理导航)-

import {DrawerItems, DrawerNavigation} from 'react-navigation';
export default DrawerNavigator({
// ... your screens
}, {
// define customComponent here
contentComponent: (props) =>
<View style={{flex: 1}}>
<Text>Header</Text>
<ScrollView>
<DrawerItems {...props} />
</ScrollView>
</View>
});

这将创建一个固定的 header,其下方的菜单项具有滚动视图。

方法二.

创建您自己的自定义组件 -

import { DrawerNavigation } from 'react-navigation'
export default DrawerNavigator({
// ... your screens
}, {
// define customComponent here
contentComponent: props => <SideMenu {...props}>
});

这里的 SideMenu 是你自己的组件,可以显示在抽屉里。您可以使用 react-navigation NavigationActions.navigate(screen) 来处理菜单项 onPress 上的路由。

第二种方法更详细的解释https://medium.com/@kakul.gupta009/custom-drawer-using-react-navigation-80abbab489f7

嵌套的导航器应该是这样的:

const Router = StackNavigator({
    Home: {screen: HomeScreen},
    Test: {screen: TestScreen}
}, {
    navigationOptions: {
        headerStyle: {backgroundColor: '#2980b9'},
        headerTintColor: '#fff'
    }
});

const Drawer = DrawerNavigator({
    App: {screen: Router}
});

对于 ui:

1) https://github.com/GeekyAnts/native-base-react-navigation-stack-navigator/blob/master/src/SideBar/SideBar.js

2) https://github.com/GeekyAnts/native-base-react-navigation-stack-navigator/blob/master/src/HomeScreen/index.js