React Navigation 5标头重叠

戴维·米歇洛蒂

我正在React Native中创建一个应用程序,正在使用React Navigation 5.x,并且用我创建的默认标头替换了默认标头,这是代码的一部分:

App.js

    <NavigationContainer>
      <Drawer.Navigator initialRouteName="Home" drawerContent={SideMenu} drawerStyle={{
    backgroundColor: '#fff',
    width: Dimensions.get('window').width - 120,
  }}>
        <Drawer.Screen name="Home" component={StackNav} />
      </Drawer.Navigator>
    </NavigationContainer>

StackNav.js

    <Stack.Navigator   headerMode="float" screenOptions={{
      cardShadowEnabled: false,
      cardOverlayEnabled:false,
      headerTransparent: true,
      headerBackTitleVisible: false,
      gestureEnabled: true,
      headerTintColor: currentTheme.colors.primary,
      headerTitleStyle: styles.headerTitle,
      gestureDirection:"horizontal",
      headerStyleInterpolator: HeaderStyleInterpolators.forStatic,
      cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
        header: ({ scene, previous, navigation }) => {
        const { options } = scene.descriptor;
        const title =
          options.headerTitle !== undefined
            ? options.headerTitle
            : options.title !== undefined
            ? options.title
            : scene.route.name;

        return (
            <MyHeader click={() => addCart()} ref={myRef} scene={scene} onPress={navigation.goBack} navigation={navigation}/>
        );
      }
      }}>
        <Stack.Screen name="Home" component={Home} options={{title: 'Home'}}/>
        <Stack.Screen name="Menu" component={RestaurantMenu} options={({ route }) => ({ title: route.params.name })}/>
        <Stack.Screen name="Piatti" component={MenuItems} options={({ route }) => ({ title: route.params.name })}/>
        <Stack.Screen name="Carrello" component={Cart} options={({ route }) => ({ title: route.params.name })}/>
      </Stack.Navigator>

MyHeader.js

import * as React from 'react';
import { StyleSheet, Button, SafeAreaView, TouchableOpacity, Image,View, Text } from 'react-native';  



const styles = StyleSheet.create({
    containerSafe: {
        flex: 1,
        flexDirection:"row",
        justifyContent: "space-between"
      },
      containerLeft: {
        zIndex: 1,
        alignSelf: 'flex-start',
        left: 0,
        marginTop:25,
        marginLeft:10,
        width: 25,
        height: 25,
        justifyContent: 'center',
        alignContent: 'center'
      },
      containerCenter: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'
      },
      containerRight: {
        zIndex: 1,
        alignSelf: 'flex-end',
        right: 0,
        marginTop:25,
        marginRight:10,
        width: 25,
        height: 25,
        justifyContent: 'center',
        alignContent: 'center'
      } 
  });

class CartButton extends React.Component {
    constructor(props) {
        super(props);
    }

    render(){
        return (
            <TouchableOpacity onPress={() => this.props.onPress()}  style={styles.containerRight}>
            <Image style={{width:"100%", height:"100%"}}
                source={require('../assets/cart.png')}
            />
            <Text>{this.props.nCart}</Text>
        </TouchableOpacity>
        )
    }
}

class HomeButton extends React.Component {
    constructor(props) {
        super(props);
    }

    render(){
        return (
            <TouchableOpacity onPress={() => this.props.onPress()} style={styles.containerLeft}>
                <Image style={{width:"100%", height:"100%"}}
                    source={require('../assets/menu.png')}
                />
            </TouchableOpacity>
        )
    }
}

class BackButton extends React.Component {
    constructor(props) {
        super(props);
    }

    render(){
        return (
            <TouchableOpacity onPress={() => this.props.onPress()} style={styles.containerLeft}>
                <Image style={{width:"100%", height:"100%"}}
                    source={require('../assets/leftArrow.png')}
                />
            </TouchableOpacity>
        )
    }
}


  class MyHeader extends React.Component {
    constructor(props) {
        super(props);
        this.state= {
            nCart: 0
        }
        this.addCart = this.addCart.bind(this);
    }

    addCart(){
        var current = this.state.nCart;
        this.setState({
            nCart: current +1
        })
    }


    render(){
        return(
            <SafeAreaView style={styles.containerSafe}>
                {(this.props.scene.route.name === "Home")? <HomeButton onPress={() =>this.props.navigation.openDrawer()} /> : <BackButton onPress={() => this.props.navigation.goBack()} />}
                <Button title="click" onPress={(() => this.props.click())} />
                <CartButton nCart={this.state.nCart} onPress={() => this.props.navigation.navigate('Carrello', {name: 'Carrello'})} />
            </SafeAreaView>
        )
    }
}

export default MyHeader;

但这给了我这个问题

在此处输入图片说明

当我切换屏幕时,“后退”按钮和“购物车”按钮会重叠,但汉堡菜单应该消失并成为“后退”按钮的箭头,而购物车应该像在家里一样在柜台上简单地更新。

satya164

https://reactnavigation.org/docs/zh-CN/stack-navigator.html#header

使用自定义标头时,建议将headerMode导航器上的prop设置为screen这样就不必实现动画了。

如果您希望自定义标题与屏幕过渡配合动画,并希望保持headerModefloat,则可以在scene.progress.currentscene.progress.next道具上进行插值

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React Navigation 5-标头未显示

React Navigation 6.0 双标头问题

React Navigation 5:在BottomTabNavigator上实现自定义标头

React Native,更改React Navigation标头样式

标头能否在react-navigation中滚动显示内容?

如何从 react-navigation 标头调用函数?

嵌套在Stack Navigation v5中的React Native Tab-更新标头的最佳实践

使用React Native中的React Navigation在navigationOptions中将标头设置为null?

标头未显示在react-navigation-drawer React-Native中

使用抽屉导航时,React Navigation标头不可见

React Navigation获取堆栈头高度

react-native React-Navigation头按钮testID

在React Navigation 5中传递参数

React Navigation 5-如何从headerRight导航?

如何使用React Navigation 5捕获参数?

React Navigation 5,未定义的参数

Android Deep Link与React Navigation 5

React Navigation 5的标题标题问题

React Navigation 5隐藏抽屉项

React Navigation 5:超过最大更新深度

删除Header React Navigation v5

获取TypeError FlatList React Navigation 5

在类中存在的标头中使用“导航”和“路线”-React-navigation v5

React Navigation v5,将堆栈导航添加到我的应用程序标头中

React Native Navigation(ver4.x)无法在自定义标头组件中获取道具。不断收到未定义的错误

React Navigation 5错误绑定元素'navigation'隐式具有'any'类型.ts

如何在React Navigation 5的Tab Navigation中传递空屏幕

如何根据状态更新React Navigation 5 headerRight中的文本?

在React Navigation v5中传递道具的问题