React Native 中底部导航的边框底部 [英] Border bottom on Bottom Navigation in React Native

查看:57
本文介绍了React Native 中底部导航的边框底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 React Native 的新手.我正在制作底部导航栏.我想在选择选项卡时添加边框底部,如下图所示.根据文档,indicatorStyle 仅适用于顶部导航.我不确定该怎么做.

I am new to React Native. I am making a bottom navigation bar. I want to add border bottom when the tab is selected, like the picture below. According to the docs, the indicatorStyle is only applicable on Top Navigation. I am unsure on how to do it.

import {
  createBottomTabNavigator,
} from 'react-navigation';
import SettingsScreen from '../settings';
import rootStyles from '../../rootStyles';
const ICON_HOME = require('../../../assets/icon/home.png');
const ICON_USER = require('../../../assets/icon/user_selected.png');
const ICON_CURRENCY = require('../../../assets/icon/currency.png');


export default createBottomTabNavigator({
  HomeScreen: {
    screen: SettingsScreen,
  },
  SettingsScreen: {
    screen: SettingsScreen,
  },
  CurrencyScreen: {
    screen: SettingsScreen,
  },
}, {
  navigationOptions: ({ navigation }) => ({
    tabBarIcon: ({ tintColor }) => {
      const { routeName } = navigation.state;
      let icon;

      switch (routeName) {
        case 'HomeScreen':
          icon = ICON_HOME;
          break;
        case 'SettingsScreen':
          icon = ICON_USER;
          break;
        case 'CurrencyScreen':
          icon = ICON_CURRENCY;
          break;
        default: break;
      }

      return (
        <Image
          source={icon}
          style={{
            height: rootStyles.em(2),
            width: rootStyles.em(2),
            tintColor,
          }}
        />
      );
    },
  }),
  tabBarPosition: 'bottom',
  tabBarOptions: {
    showLabel: false,
    activeTintColor: rootStyles.PRIMARY_COLOR,
  },
  tabBarSelectedItemStyle: {
    borderBottomWidth: 2,
    borderBottomColor: 'red',
  },
});

推荐答案

您可以为bottomTabNavigator 做一个自定义选项卡.我有一个在做一个项目,看看:

You can do a custom tab for bottomTabNavigator. I have one of these working on a project, take a look:

import {createBottomTabNavigator} from "react-navigation-tabs";
import { CustomTab } from '../../common/components';
...

    const Tabs = createBottomTabNavigator({
      Home: {
        screen: HomeStack,
      },
      Events: {
        screen: EventStack,
      },
      Templates:{
        screen: TemplateStack
      }
    }, {
      initialRouteName: 'Home',
      tabBarOptions: {
        activeTintColor: theme.palette.primaryColor,
      },
      tabBarComponent: CustomTab,
      tabBarPosition: 'bottom',
    });

和我的 CustomTab 组件:

and my CustomTab component:

import React, {PureComponent} from 'react'
import {View, TouchableWithoutFeedback, Text, StyleSheet} from 'react-native'
import I18N from "react-native-i18n";
import * as Animatable from 'react-native-animatable';
import theme from '../theme'
import {Icon} from './Icon'

class CustomTab extends PureComponent {

  constructor(){
    super()
    this.state = {
      routeNameSelected:'Home'
    }
  }

  getIconName(routeName){
    switch (routeName) {
      case 'EventInfoTab':
        return 'information-outline'
      case 'EventChannelTab':
        return 'play-box-outline'
      case 'EventCommentTab':
        return 'comment-text-outline'
      case 'Home':
        return 'home'
      case 'Events':
        return 'calendar-star'
      case 'Templates':
        return 'clipboard-text'
    }
  }

  onPressTab(routeName){
    this.props.jumpTo(routeName)
    this.setState({
      routeNameSelected:routeName
    })
  }

  render() {
    const {navigation} = this.props;
    const {routes} = navigation.state;
    return (
        <View style={styles.tabbar}>
          {routes && routes.map((route, index) => {
            return (
              <TouchableWithoutFeedback
                key={route.key}
                style={styles.tab}
                onPress={() => this.onPressTab(route.routeName)}
              >
                <View style={{minHeight:50, justifyContent:'center'}}>
                  {navigation.state.index===index &&
                  <Animatable.View animation="rubberBand" duration={1000} style={styles.tab}>
                    <Icon size={24} name={this.getIconName(route.routeName)} style={{ color: theme.palette.primaryColor }} />
                    <Text style={[styles.tabText,{color: theme.palette.primaryColor}]}>{I18N.t('tabs.'+route.routeName)}</Text>
                  </Animatable.View>
                  }
                  {navigation.state.index!==index &&
                  <View style={styles.tab}>
                    <Icon size={24} name={this.getIconName(route.routeName)} style={{ color: theme.palette.colors.titleColor }} />
                    <Text style={[styles.tabText,{color: theme.palette.colors.titleColor}]}>{I18N.t('tabs.'+route.routeName)}</Text>
                  </View>
                  }
                </View>
              </TouchableWithoutFeedback>
            );
          })}

        </View>
    )
  }
}

export {CustomTab}

在 CustomTab 组件上为底层选项卡和根据此状态呈现的视图定义状态.如果这对您有帮助,请告诉我.

Define a state for underlying tab and the view rendered according to this state on CustomTab component. Let me know if this is helpful for you.

这篇关于React Native 中底部导航的边框底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆