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

发布于 2019-01-14 00:35:21
1.获取导入的图标库:-
import Icon from 'react-native-vector-icons/AwesomeFont'2.使用道具创建一个根据路由渲染图标的方法:-
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:-
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的样式(这里的标签是隐藏的,只使用图标标签)
const styles = StyleSheet.create({
scene: {
flex: 1,
},
noLabel: {
display: 'none',
height: 0
},
bubble: {
backgroundColor: 'lime',
paddingHorizontal: 18,
paddingVertical: 12,
borderRadius: 10
},
})react-native
发布于 2018-07-06 21:49:52
您必须覆盖renderHeader方法并在TabBar中定义您自己的呈现标签方法:
renderHeader = (props) => (
<TabBar
style={styles.tabBar}
{...props}
renderLabel={({ route, focused }) => (
<View style={styles.tabBarTitleContainer}>
/* HERE ADD IMAGE / ICON */
</View>
)}
renderIndicator={this.renderIndicator}
/>
);发布于 2018-12-11 01:49:25
我也有同样的问题。我通过创建一个"_renderTabBar“函数并将其作为道具传递给TabView组件的renderTabBar方法来解决这个问题,在这个函数中,我将组件"image”作为我的图标,我希望它能有所帮助。
一张照片:

_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>
);};
你可以在这里渲染
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
https://stackoverflow.com/questions/51211723
复制相似问题