React-native:如何使用Custom TabBarComponent获取navigationOptions

时间:2017-08-31 12:38:34

标签: react-native

我是新来回应本机,我正在尝试构建一个自定义标签栏,但我在尝试显示图标标签栏时遇到问题。 这是我到目前为止取得的成就。 enter image description here

这是我的自定义TabBar组件:

class TabBar extends Component {
    renderItem = (route, index) => {
      const {
        navigation,
        jumpToIndex,
      } = this.props;

      const isCapture = route.routeName === 'AddExpenses';

      const focused = index === navigation.state.index;
      const color = focused ? activeTintColor : inactiveTintColor;
      if (isCapture === true) {
        return (
          <TouchableOpacity
            key={route.key}
            style={Styles.tab}
            onPress={() => (navigation.navigate('AddExpensesModal'))}
          >
            <Ionicons
              name={ioniconsByPlatform('add-circle')}
              style={Styles.icon}
              size={26}
            />
          </TouchableOpacity>
        );
      }
      return (
        <TouchableWithoutFeedback
          key={route.key}
          style={Styles.tab}
          onPress={() => (isCapture ? navigation.navigate('CaptureModal') : jumpToIndex(index))}
        >
          <View style={Styles.tab}>
            <Text style={{ color }}>{route.routeName}</Text>
          </View>
        </TouchableWithoutFeedback>
      );
    }

    render() {
      const {
        navigation,
      } = this.props;

      const {
        routes,
      } = navigation.state;

      return (
        <View style={Styles.tabBar}>
          {routes && routes.map(this.renderItem)}
        </View>
      );
    }
}
export default TabBar;

我的标签浏览器:

const MainTabNavigator = TabNavigator({
  Summary: { screen: SummaryScreen },
  AddExpenses: { screen: ExpensesScreen },
  Expenses: { screen: ExpensesScreen },
}, {
  tabBarComponent: TabBar,
});

export default MainTabNavigator;

我尝试设置TabBarIcon的屏幕示例:

const SummaryScreen = () => (
  <View style={Styles.container}>
    <Text>Summary</Text>
  </View>
);


SummaryScreen.navigationOptions = {
  title: 'Summary',
  tabBarIcon: props => <TabBarIcon {...props} name="pulse" />,
};

export default SummaryScreen;

由于navigationOptions属性,我希望能够显示我的标签栏图标。 你知道我怎么能这样做吗?

1 个答案:

答案 0 :(得分:1)

如果您认为TabNavigator不够强大(我认为它远非强大),您可以随时自定义导航视图。

以下是我自定义导航器视图以替换TabNavigator的说明:

    export default class SectionTabView extends React.Component {

    static propTypes = {
        navigation: PropTypes.object
    };

    constructor(props) {
        super(props);
    }

    render() {
        const {router, navigation} = this.props;
        const {routes, index} = navigation.state;

        /**
         * ActiveScreen is the current screen you see when you change you navigation state in tab bar
         */
        const ActiveScreen = router.getComponentForState(navigation.state);

        return (
            <View style={Styles.section_container}>
                <ActiveScreen
                    navigation={addNavigationHelpers({
                        ...navigation,
                        state: routes[index],
                    })}
                />
                <SectionTabBar navigation={navigation}/>
            </View>
        );
    }
}

export default class SectionTabBar extends React.Component {

    static propTypes = {
        navigation: PropTypes.object
    };

    constructor(props) {
        super(props);
    }

    getTabButtomGroupView() {
        const {navigation} = this.props;
        const {routes, index} = navigation.state;

        let tabButtomGroupView = [];

        routes.map((route) => {
            let styles = [Styles.eventSection_tab];
            const isClicked = routes[index].routeName === route.routeName;

            if(isClicked){
                styles.push(Styles.eventSection_tabClicked);
            }

            tabButtomGroupView.push(
                <TouchableOpacity
                    onPress={() => {
                        /**
                         * when the routeName is equal to current routeName, we should stop navigate action
                         */
                        if (routes[index].routeName === route.routeName) {
                            return;
                        }
                        navigation.navigate(route.routeName);
                    }}
                    style={styles}
                    key={route.routeName}>

                    <Text style={{color:'white'}}>{SectionRouteConfig[route.routeName].navigationOptions.title}</Text>

                </TouchableOpacity>
            )
        });

        return tabButtomGroupView;
    }

    render() {

        return (
            <View style={Styles.section_tabContainer}>
                {this.getTabButtomGroupView()}
            </View>
        );
    };
}

//SectionRouteConfig.js
export const sectionRouteConfig = {
    XXX: {
        screen: XXX, navigationOptions: {
            title: XXX
        }
    },
    XXX: {
        screen: XXX, navigationOptions: {
            title: XXX
        }
    }
};

export const SectionNavigator = createNavigator(TabRouter(sectionRouteConfig))(SectionTabView);

//Usage
render() {
        const {dispatch, navigationState} = this.props;
        return (
            <SectionNavigator
                navigation={
                    addNavigationHelpers({
                        dispatch: dispatch,
                        state: navigationState
                    })
                }
            />
        )
    }

顺便说一句,我也使用redux。

如果这些代码太多,您可以在此处查看官方示例:pricing