代码之家  ›  专栏  ›  技术社区  ›  fun joker

如何在React Native中切换抽屉导航器?

  •  0
  • fun joker  · 技术社区  · 5 年前

    我正在使用抽屉导航。当我点击汉堡包菜单图标时,抽屉导航的抽屉不工作,我无法切换抽屉。代码1不起作用,但代码2起作用,有人能帮我找出原因吗?

    代码:

        Burger menu icon code 1:
    
        // Inside Header
        navigationOptions: ({ navigation }) => ({
                    // headerTitle: 'Rchampz',
                    headerLeft:
                    <TouchableOpacity  onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())} }>
                        <MenuImage navigation={navigation}/>
                    </TouchableOpacity>,
    
    //Created component and added inside navigationOptions
    
        const MenuImage = ({navigation}) => {
            if(!navigation.state.isDrawerOpen){
                return <TouchableOpacity><Image source={require('../assets/images/menu.png')} style={{width: 24, height: 24, resizeMode: 'contain' ,marginLeft: 15}}/></TouchableOpacity>
            }else{
                return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-arrow-back" size={30} color="black"/>
            }
        }
    
        Burger menu icon code 2:
    
        //Inside header
    
        navigationOptions: ({ navigation }) => ({
                    // headerTitle: 'Rchampz',
                    headerLeft:
                    <TouchableOpacity  onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())} }>
                        <MenuImage navigation={navigation}/>
                    </TouchableOpacity>,
    
    
    //Created component and added inside navigationOptions
    
        const MenuImage = ({navigation}) => {
            if(!navigation.state.isDrawerOpen){
                return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-menu" size={30} color="black"/>
            }else{
                return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-arrow-back" size={30} color="black"/>
            }
        }
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   Sean Wang    5 年前

    因为您在第一个菜单中设置了菜单图像。你把图像包装在另一个 TouchableOpacity 这意味着 可触摸不透明度 正在接收用户触摸事件。这不是你想要的行为,因为你想要 可触摸不透明度 在HeaderLeft上作为输入的接收器。所以只需去掉“多余的” 可触摸不透明度 .

    // Inside Header
    navigationOptions: ({ navigation }) => ({
        headerLeft:
        <TouchableOpacity onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())} }>
            <MenuImage navigation={navigation}/>
        </TouchableOpacity>,
    
    //Created component and added inside navigationOptions
    const MenuImage = ({navigation}) => {
        if(!navigation.state.isDrawerOpen){
            // No need of Touchable here. Since you want the above Touchable to pick up the onPress event.
            return <Image source={require('../assets/images/menu.png')} style={{width: 24, height: 24, resizeMode: 'contain' ,marginLeft: 15}}/>. 
        }else{
            return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-arrow-back" size={30} color="black"/>
        }
    }
    

    第二个例子之所以有效,只是因为没有额外的 Touchable 处理OnPress事件。