虽然这可以追溯到很久以前,但我想出了一个解决方法,至少它的目标是表现得像一个底部抽屉。
我的第一次尝试也是修改抽屉组件以从不同的方向向上滑动。我和你一样挣扎。绝望地浏览文档时,我幸运地偶然发现了React Navigation 本身提供的透明模态演示。
底部抽屉视图:
import React from 'react';
import {Animated, Pressable, StyleSheet, View} from 'react-native';
import {useNavigation} from '@react-navigation/core';
import {useTheme} from '@react-navigation/native';
import {useCardAnimation} from '@react-navigation/stack';
export interface IBottomDrawer {}
const BottomDrawer: React.FC<IBottomDrawer> = () => {
const navigation = useNavigation();
const {colors} = useTheme();
const {current} = useCardAnimation();
const styles = StyleSheet.create({
bottomView: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
},
modalView: {
padding: 16,
paddingTop: 0,
width: '100%',
borderTopStartRadius: 20,
borderTopEndRadius: 20,
backgroundColor: colors.card,
transform: [
{
translateY: current.progress.interpolate({
inputRange: [0, 1],
outputRange: [100, 0],
}),
},
],
},
});
return (
<View style={styles.bottomView}>
<Pressable style={StyleSheet.absoluteFill} onPress={navigation.goBack} />
<Animated.View style={styles.modalView}>
<!-- Add Drawer Items that use navigation.navigate('to-my-route') -->
</Animated.View>
</View>
);
};
export default BottomDrawer;
StackNavigator 使用提到的透明模式表示:
<MainStack.Screen name="Root" component={RootNavigator} />
<MainStack.Screen
name="BottomDrawer"
component={BottomDrawer}
options={{presentation: 'transparentModal', cardOverlayEnabled: true}}
/>