[英]How to create Custom Top tab bar in React Native using react-navigation?
您可以找到许多教程来创建自定义标签栏。 使用 createMaterialTopTabNavigator 创建顶栏。
import { createMaterialTopTabNavigator, createAppContainer } from "react-navigation"
import TabBar from './TabBar'
const TabNavigator = createMaterialTopTabNavigator({
feed: {
screen: () => null,
},
profile: {
screen: () => null,
},
inbox: {
screen: () => null,
}
}, {
tabBarComponent: TabBar,
})
export default createAppContainer(TabNavigator)
TabBar 组件
import * as React from "react"
import { View } from "react-native"
import Tab from './Tab'
const TabBar = (props) => {
const { navigationState, navigation, position } = props
return (
<View style={{
height: 80,
backgroundColor: 'seashell',
flexDirection: "row",
justifyContent: 'space-around',
alignItems: 'center',
}}>
{navigationState.routes.map((route, index) => {
const focusAnim = position.interpolate({
inputRange: [index - 1, index, index + 1],
outputRange: [0, 1, 0]
})
return (
<Tab
focusAnim={focusAnim}
title={route.routeName}
onPress={() => navigation.navigate(route.routeName)}
/>
)
})}
</View>
)
}
export default TabBar
选项卡组件
import * as React from "react"
import { Animated, TouchableOpacity } from "react-native"
const Tab = ({ focusAnim, title, onPress }) => {
return (
<TouchableOpacity onPress={onPress}>
<Animated.View
style={{
padding: 10,
borderRadius: 10,
backgroundColor: focusAnim.interpolate({
inputRange: [0, 1],
outputRange: ["transparent", "tomato"]
})
}}
>
<Animated.Text
style={{
color: focusAnim.interpolate({
inputRange: [0, 1],
outputRange: ["#444", "#fff"]
})
}}
>{title}</Animated.Text>
</Animated.View>
</TouchableOpacity>
)
}
export default Tab
您需要做的是根据您的需要对其进行样式设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.