React Native中底部导航的边框底部

时间:2018-11-06 09:04:29

标签: react-native react-navigation

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

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',
  },
});

1 个答案:

答案 0 :(得分:0)

您可以为bottomTabNavigator做一个自定义选项卡。我有一个正在研究项目的人,看看:

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组件:

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组件上根据此状态定义视图。让我知道这是否对您有帮助。