How to get drawer over the header in react navigation?

假装没事ソ 提交于 2019-12-04 04:26:28
  1. You should create a new StackNavigator for your CategoryScreen and ProductScreen
  2. You set the header on CategoryScreen and ProductScreen navigation options

Here is what i meant

// wrap your screen inside the drawer into StackNavigator
const CategoryNavigator = createStackNavigator({
  CategoryList: {
    screen: CategoryScreen,
    navigationOptions: {
      title: "Category",
      header: // any custom header here
    }
  },
});

const drawerScreens = createDrawerNavigator({
  Category: CategoryNavigator,
  Products: ProductNavigator,
}, {
  initialRouteName: 'Category'
})


export default AppStack = createStackNavigator({
  drawer: {
    screen: drawerScreens,
  },
  cart: {
    screen: CartScreen
  }
}, {
  initialRouteName: 'drawer',
});

This is the result

Following will make a floating header which similar with your screenshot

Set the header mode to float (you don't need to wrap CategoryScreen and ProductScreen into StackNavigator)

export default AppStack = createStackNavigator({
  drawer: {
    screen: drawerScreens,
  },
  cart: {
    screen: CartScreen
  }
}, {
  headerMode: 'float', // set this header mode to float so you can share the header
  initialRouteName: 'drawer',
});

This is the result if you change the header mode to float

In my case, i make my own Header component and use it in each page i want. it enabled me to customize header with each page.

Absolutely it is the back door way and i hope other people have the exact answer of your question.

This is an example...

Home page:

export default class Home extends Component {
    render() {  
        return (
            <View style={{ flex: 1 }}>

                <Header showBorder={true}/>

                <ScrollView>
                   ...
                </ScrollView>
            </View>
        );
    }
}

Header Component:

export default class Header extends React.PureComponent {

  renderTitle = () => {
    if (this.props.title) {
      return (
        <View style={{ flexDirection: 'column', flex: 3, justifyContent: 'center' }}>
          <View style={{ alignSelf: 'flex-start' }}>
            <Text style={[styles.fontBold, { fontSize:17, color: colors.borderWidthColor }]}>{this.props.title}</Text>
          </View>
        </View>
      )
    }
  }

  renderBack = () => {
    if (this.props.back) {
      return (
        <View style={{ marginTop:3 }}>
          <TouchableOpacity
            onPress={() => {
              this.props.navigation.goBack()
            }}
            style={{ alignSelf: 'flex-start' }}>
            <Icon name="md-arrow-back" size={23} color="black" />
          </TouchableOpacity>
        </View>
      )
    }
  }


  render() {
    return (
      <View style={[{ height: 70, backgroundColor: this.props.backgroundColor, width: win.width, borderBottomWidth: this.props.showBorder ? 1 : 0 }]}>
        <View style={{ flex: 1, flexDirection: 'row', marginTop: Platform.OS == 'ios' ? 17 : 3 }}>
          <View style={{ flexDirection: 'column', flex: 1, justifyContent: 'center', marginLeft: 12 }}>
            {this.renderBack()}
            {this.renderTitle()}
          </View>
        </View>
      </View>
    )
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!