如何在React Native中将屏幕添加到自定义底部导航

时间:2020-10-21 14:45:12

标签: react-native react-navigation-v5 react-navigation-bottom-tab

我是本机反应新手,并试图设计一个自定义的底部导航栏,如图hereSource code

标签栏设计已成功创建,但是我对如何在单击按钮时更改屏幕感到困惑。 基本上,我无法理解如何将React Native Navigation Component连接到此自定义底部标签栏。

我期待使用React navigation custom navbar support ...,但不确定如何实现。

请帮助。

谢谢。

2 个答案:

答案 0 :(得分:0)

我从未尝试过这样做,但是您可以通过使用默认的堆栈导航器来做到这一点:https://reactnavigation.org/docs/navigating 那么,想法是什么。您创建5个不同的屏幕,并在每个屏幕上复制此选项卡。然后,使用navigation.navigate属性为每种可能的情况编写条件语句。我认为这是唯一的方法,但我不知道这将如何影响应用程序的性能

答案 1 :(得分:0)

反应导航文档确实很有帮助。查看我的解决方案here

解决方案是仅在常规导航器定义中添加自定义选项卡栏设计的引用,并将状态,描述符,导航道具从导航器传递到自定义设计,如下所示。

/components/BottomBar/index.js :定义导航模型并将Tabbar用作自定义设计。

import * as React from "react";
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { View } from "react-native";
import  TabBar  from "./Tabbar";
import Screen1 from "../../screens/Screen1";
import Screen2 from "../../screens/Screen2";


export const BottomMenu = () => {
  const Tab = createBottomTabNavigator();
  return (
    
    <View style={{ flex: 1, position: "relative",  justifyContent: 'flex-end'}}>
    
      <Tab.Navigator
        tabBar={(props) => <TabBar {...props} />}
      >
        <Tab.Screen name="screen1" component={Screen1} />
        <Tab.Screen name="screen2" component={Screen2} />
        <Tab.Screen name="screen3" component={Screen1} />
        <Tab.Screen name="screen4" component={Screen2} />
      </Tab.Navigator>     
  
    </View>
   
  );
};

/components/BottomBar/TabBar.js :将导航器道具以及自定义图标详细信息传递到静态选项卡栏。

const { state, descriptors, navigation } = this.props
 :
<StaticTabbar {...{ tabs, value, state, descriptors, navigation }} />  

/components/BottomBar/StaticTabbar.js :使用道具在标签栏中显示项目

    const { tabs, value } = this.props;
    const { state, descriptors, navigation } = this.props
    
    return (
      <View style={styles.container}>
        {
          state.routes.map((route, index) => {
            const { options } = descriptors[route.key];
            const tabOption = tabs[index];
              :               
            const key = index;
            return (
              <React.Fragment {...{ key }}>
                <TouchableWithoutFeedback onPress={() => onPress(index)}>
                  <Animated.View style={[styles.tab, { opacity }]}>
                    <Icon name={tabOption.name} color="white" size={28} />
                    <Text style={{ fontSize: 11, color: 'white' }}>{tabOption.text}</Text>
                  </Animated.View>
                </TouchableWithoutFeedback>
                <Animated.View
                  style={{
                    position: "absolute",
                    top: -7,
                    left: tabWidth * index,
                    width: tabWidth,
                    height: 64,   // Tab bar height
                    justifyContent: "center",
                    alignItems: "center",
                    opacity: opacity1,
                    transform: [{ translateY }],
                  }}
                >
                  <View style={styles.activeIcon}>
                    <Icon name={tabOption.name} color="#004c40" size={25} />
                  </View>
                </Animated.View>
              </React.Fragment>
            )
          })}
      </View>
      );