How to get drawer over the header in react navigation?

后端 未结 2 1665
孤街浪徒
孤街浪徒 2021-02-20 01:15

I am using react navigation. I want to show drawer over the header of the screen. Currently my header is not hiding below drawer when I open the drawer.

相关标签:
2条回答
  • 2021-02-20 01:51

    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>
        )
      }
    }
    
    0 讨论(0)
  • 2021-02-20 01:57
    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

    0 讨论(0)
提交回复
热议问题