首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将图标添加到react-native-tab-view中的选项卡

如何将图标添加到react-native-tab-view中的选项卡
EN

Stack Overflow用户
提问于 2018-07-06 21:40:39
回答 3查看 8.1K关注 0票数 5

我正在使用react-native-tab-view,我可以将文本放在标签上,但我想要图标。在GitHub上有一些答案,但它们并不清楚。如果你知道什么的话,那就太完美了!

EN

回答 3

Stack Overflow用户

发布于 2019-01-14 00:35:21

1.获取导入的图标库:-

代码语言:javascript
复制
import Icon from 'react-native-vector-icons/AwesomeFont'

2.使用道具创建一个根据路由渲染图标的方法:-

代码语言:javascript
复制
const getTabBarIcon = (props) => {

    const {route} = props

      if (route.key === 'Search') {

       return <Icon name='search' size={30} color={'red'}/>

      } else {

       return <Icon name='circle' size={30} color={'red'}/>

      }
}

3.使用渲染的TabBar属性回调getTabBarIcon的 Render TabView:-

代码语言:javascript
复制
export default class App extends React.Component {

    state = {
        index: 0,
        routes: [
            {key: 'Home', title: 'Hello'},
            {key: 'Search', title: 'Second'}
        ],
    }

    render() {
        return (
            <TabView
                navigationState={this.state}
                renderScene={SceneMap({
                    Home: FirstRoute,
                    Search: SearchScreen,
                })}
                onIndexChange={index => this.setState({index})}
                initialLayout={{height: 100, width: Dimensions.get('window').width}}
                renderTabBar={props =>
                    <TabBar
                        {...props}
                        indicatorStyle={{backgroundColor: 'red'}}
                        renderIcon={
                            props => getTabBarIcon(props)
                        }
                        tabStyle={styles.bubble}
                        labelStyle={styles.noLabel}
                    />
                }
                tabBarPosition={'bottom'}
            />
        );
    }
}

4.你可以用任何东西设置TabBar的样式(这里的标签是隐藏的,只使用图标标签)

代码语言:javascript
复制
const styles = StyleSheet.create({
    scene: {
        flex: 1,
    },
    noLabel: {
        display: 'none',
        height: 0
    },
    bubble: {
        backgroundColor: 'lime',
        paddingHorizontal: 18,
        paddingVertical: 12,
        borderRadius: 10
    },
})

react-native

票数 7
EN

Stack Overflow用户

发布于 2018-07-06 21:49:52

您必须覆盖renderHeader方法并在TabBar中定义您自己的呈现标签方法:

代码语言:javascript
复制
  renderHeader = (props) => (
      <TabBar
        style={styles.tabBar}
        {...props}
        renderLabel={({ route, focused }) => (
          <View style={styles.tabBarTitleContainer}>
               /* HERE ADD IMAGE / ICON */
          </View>
        )}
        renderIndicator={this.renderIndicator}
      />
  );
票数 0
EN

Stack Overflow用户

发布于 2018-12-11 01:49:25

我也有同样的问题。我通过创建一个"_renderTabBar“函数并将其作为道具传递给TabView组件的renderTabBar方法来解决这个问题,在这个函数中,我将组件"image”作为我的图标,我希望它能有所帮助。

一张照片:

代码语言:javascript
复制
_renderTabBar = props => {
const inputRange = props.navigationState.routes.map((x, i) => i);

return (
  <View style={styles.tabBar}>
    {props.navigationState.routes.map((route, i) => {
      const color = props.position.interpolate({
        inputRange,
        outputRange: inputRange.map(
          inputIndex => (inputIndex === i ? 'red' : 'cyan')
        ),
      });
        return (
        <TouchableOpacity
          style={[styles.tabItem, {backgroundColor: '#FFF' }]}
          onPress={() => this.setState({ index: i })}>
          <Image
      style={styles.iconTab}
      source={{uri: 'https://www.gstatic.com/images/branding/product/2x/google_plus_48dp.png'}}
    />
          <Animated.Text style={{ color }}>{route.title}</Animated.Text>

        </TouchableOpacity>

      );
    })}
  </View>
);

};

你可以在这里渲染

代码语言:javascript
复制
render() {
return (
  <TabView
    navigationState={this.state}
    renderScene={this._renderScene}
    renderTabBar={this._renderTabBar}
    onIndexChange={index => this.setState({ index })}
  />
);

代码完整:https://snack.expo.io/@brunoaraujo/react-native-tab-view-custom-tabbar

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51211723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档