1. 获取导入的图标库:-
import Icon from 'react-native-vector-icons/AwesomeFont'
2. 创建一个使用 props 根据路线渲染图标的方法:-
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. 使用回调 getTabBarIcon 的渲染 TabBar 渲染 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
},
})
您必须重写 renderHeader 方法并在 TabBar 中定义您自己的渲染标签方法:
renderHeader = (props) => (
<TabBar
style={styles.tabBar}
{...props}
renderLabel={({ route, focused }) => (
<View style={styles.tabBarTitleContainer}>
/* HERE ADD IMAGE / ICON */
</View>
)}
renderIndicator={this.renderIndicator}
/>
);
我也有同样的问题。我通过创建一个“_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
您也可以在路由数组中传递图标名称并直接使用它
const renderScene = SceneMap({
first: Gallery,
second: Camera
})
export default function App() {
const layout = useWindowDimensions();
const [index, setIndex] = useState(0);
const [routes] = useState([
{ key: "first", icon:"camera" },
{ key: "second", icon:"view-gallery"}
]);
return (
<>
<StatusBar />
<TabView
navigationState={{ index, routes }}
renderScene={renderScene}
onIndexChange={setIndex}
initialLayout={{ width: layout.width }}
renderTabBar={ props => (
<TabBar {...props}
renderIcon={ routes => (
<Icon name={routes?.route?.icon} size={30} color="#fff" />
) }
/>
) }
/>
</>
);
}