javascript - 如何将自定义组件添加到 createMaterialTopTabNavigator 选项卡栏

标签 javascript react-native react-navigation

我正在使用 createMaterialTopTabNavigator来自 react-navigation并尝试通过在其顶部添加一些组件来自定义标签栏。

正如您在此处的指南中所见:

https://reactnavigation.org/docs/en/material-top-tab-navigator.html#docsNav

有一个选项叫做 tabBarComponent可以通过它来创建您自己的标签栏。但是,它完全覆盖了我不想要的标签栏。

我想在选项卡栏的顶部添加一个自定义组件,然后将默认选项卡及其标签放在下面。

谁能告诉我如何向标签栏添加组件的示例?

例如,下面的代码将标签替换为 My Custom Component文本,但我怎样才能同时拥有它们? (自定义组件和选项卡)

const myNavigation = createMaterialTopTabNavigator({
  firstTab: FirstTabScreen,
  secondTab: SecondTabScreen,
  thirdTab: ThirdTabScreen,
},
{
  tabBarComponent: props => (
    <View><Text>My Custom Component</Text></View>
  )
});

最佳答案

创建自定义标签栏组件并不难,下面是我为我正在处理的项目创建的自定义标签栏的简化示例。

但实际上并没有那么多,你的标签栏组件接收到一个导航 Prop ,它保存了你在 createMaterialTopTabNavigator 中设置的不同路线。 .因此,您只需遍历这些路由并为每个路由显示一个标签栏项目。

CustomTabBar.jsx

export default class CustomTabBar extends React.Component {

  render() {

    const {navigation} = this.props;    
    const routes = navigation.state.routes;

    return (
      <SafeAreaView style={{backgroundColor: 'blue'}}>
        <View style={styles.container}>
          {routes.map((route, index) => {
            return (
              <View style={styles.tabBarItem}>
                <CustomTabBarIcon
                  key={route.key}
                  routeName=route.routeName
                  onPress={() => this.navigationHandler(index)}
                  focused={navigation.state.index === index}
                  index={index}
                />
          </View>
            );
          }
        </View>
      </SafeAreaView>
    );
  }

  navigationHandler = (routeName) => {
    this.props.navigation.navigate(routeName);
  }
}

const styles = StyleSheet.create({

  container: {
    flexDirection: 'row',
    alignContent: 'center',
    height: 56,
    width: '100%',
    paddingHorizontal: 16,
    backgroundColor: 'blue',
  },
  tabBarItem: {
    flex: 1,
    alignItems: 'center'
  }
});

CustomTabBarItem.jsx
class CustomTabBarIcon extends React.PureComponent {

  render() {

    const {index, focused, routeName} = this.props;
    let icon = '';

    switch (index) {
      case 0: 
        icon = 'a';
        break;

      case 1:
        icon : 'b';
        break;

      case 2:
        icon = 'c';
        break;

      default: 
        iconName = 'a';
    }

    return (
      <TouchableWithoutFeedback
        onPress={() => this.onSelect(routeName)}
      >
        <View style={[styles.container, focused ? styles.active : styles.inactive]}> 
          <View style={styles.icon}>
            <Icon name={icon} color='white' size={24}/>
          </View>
          <Text style={styles.textStyle}>{routeName}</Text>
        </View>
      </TouchableWithoutFeedback>
    );
  }

  onSelect = (routeName) => {    
    this.props.onPress(routeName);
  }
}

const styles = StyleSheet.create({

  container: {
    flex: 1,
    alignItems: 'center'
  },
  active: {
    borderTopWidth: 3,
    borderColor: 'white'
  },
  inactive: {
    borderTopWidth: 3,
    borderColor: 'blue'  
  },
  textStyle: {
    color: 'white',
    fontSize: 13
  }
});

关于javascript - 如何将自定义组件添加到 createMaterialTopTabNavigator 选项卡栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54409280/

相关文章:

javascript - 不需要的事件覆盖

performance - 我想在 native react 中更改 ScrollView 滚动速度

javascript - iOS( native react ): Unnecessary space from the top of the header rendered using react navigation

javascript - 为什么传奇效应没有被调用?

javascript - 运行 native 项目时无效的 sdkVersion

react-native - 将 Expo LinearGradient 设置为整个 React Native 应用程序的背景

react-native - 如何在 native react 中将选定的自定义复选框数据发送到下一页?

javascript - 我需要在嵌套的 require 调用中列出多少个依赖项?

javascript - 根据用户提供的值对 svg 行进行动画处理

javascript - 从多个按钮打开单个模态框