React Navigation Docs 真的很有帮助。查看我的解决方案here。
解决方案是在通常的导航器定义中添加自定义标签栏设计的引用,并将状态、描述符、导航道具从导航器传递到自定义设计,如下所示。
/components/BottomBar/index.js:定义导航模型,使用Tabbar作为自定义设计。
import * as React from "react";
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { View } from "react-native";
import TabBar from "./Tabbar";
import Screen1 from "../../screens/Screen1";
import Screen2 from "../../screens/Screen2";
export const BottomMenu = () => {
const Tab = createBottomTabNavigator();
return (
<View style={{ flex: 1, position: "relative", justifyContent: 'flex-end'}}>
<Tab.Navigator
tabBar={(props) => <TabBar {...props} />}
>
<Tab.Screen name="screen1" component={Screen1} />
<Tab.Screen name="screen2" component={Screen2} />
<Tab.Screen name="screen3" component={Screen1} />
<Tab.Screen name="screen4" component={Screen2} />
</Tab.Navigator>
</View>
);
};
/components/BottomBar/TabBar.js:将导航器道具与自定义图标详细信息一起传递给静态标签栏。
const { state, descriptors, navigation } = this.props
:
<StaticTabbar {...{ tabs, value, state, descriptors, navigation }} />
/components/BottomBar/StaticTabbar.js:使用道具显示标签栏中的项目
const { tabs, value } = this.props;
const { state, descriptors, navigation } = this.props
return (
<View style={styles.container}>
{
state.routes.map((route, index) => {
const { options } = descriptors[route.key];
const tabOption = tabs[index];
:
const key = index;
return (
<React.Fragment {...{ key }}>
<TouchableWithoutFeedback onPress={() => onPress(index)}>
<Animated.View style={[styles.tab, { opacity }]}>
<Icon name={tabOption.name} color="white" size={28} />
<Text style={{ fontSize: 11, color: 'white' }}>{tabOption.text}</Text>
</Animated.View>
</TouchableWithoutFeedback>
<Animated.View
style={{
position: "absolute",
top: -7,
left: tabWidth * index,
width: tabWidth,
height: 64, // Tab bar height
justifyContent: "center",
alignItems: "center",
opacity: opacity1,
transform: [{ translateY }],
}}
>
<View style={styles.activeIcon}>
<Icon name={tabOption.name} color="#004c40" size={25} />
</View>
</Animated.View>
</React.Fragment>
)
})}
</View>
);