你尝试过创建自己的tabBarComponent
吗?https://reactnavigation.org/docs/navigators/tab#TabNavigatorConfig
你可以复制内置的tabComponent,并添加你的ToolTip组件进行渲染。
更新
抱歉,我忘记更新我的答案了,请让我更详细地解释一下我是如何解决这个问题的。
所以我使用了自己创建的组件,使用样式位置absolute
,并使用useRef
获取要添加工具提示的组件引用,通过引用获取屏幕上的y
位置,将您的工具提示组件放在其上方或根据您拥有的逻辑进行调整,这里是假设。
// Tooltip.js
const styles = StyleSheet.create({
toolTip: (x, y) => ({
position: 'absolute',
left: x,
top: y
}),
});
const Tooltip = ({pos}) => {
return (
<View style={styles.toolTip(pos.x, pos.y)} >
<Text>Hi There!</Text>
</View>
)
}
// App.js
import {useRef} from 'react';
const bottomNavRef = useRef();
const [tooltip, setTooltip] = useState({visible: false, pos: {x: 0, y: 0}})
const showTooltip = () => {
const x = bottomNavRef.current.clientX
const y = bottomNavRef.current.clientY
setTooltip({ visible: true, pos: {x, y} })
}
...
return (
<View>
<Tooltip visible={tooltip.visible} />
<BottomNav ref={bottomNavRef}>
...
</BottomNav>
</View>
)
我知道这还没有经过测试,但我发誓这就是你可以创建自己的工具提示的整个想法。
也许你可以创建自定义的工具提示组件。以下是一个非常基本的示例,演示如何使用一些CSS技巧和zIndex属性使其显示在其他组件前面:https://codepen.io/vtisnado/pen/WEoGey
class SampleApp extends React.Component {
render() {
return (
/******************* RENDER VISUAL COMPONENTS *******************/
<View style={styles.container}>
<View style={styles.mainView}>
{/* Start: Tooltip */}
<View style={styles.talkBubble}>
<View style={styles.talkBubbleSquare}>
<Text style={styles.talkBubbleMessage}>Put whatever you want here. This is just a test message :)</Text>
</View>
<View style={styles.talkBubbleTriangle} />
</View>
{/* End: Tooltip */}
<View style={styles.anotherView} /> {/* The view with app content behind the tooltip */}
</View>
</View>
/******************* /RENDER VISUAL COMPONENTS *******************/
);
}
}
/******************* CSS STYLES *******************/
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
mainView: {
flex: 1,
flexDirection: 'row',
},
talkBubble: {
backgroundColor: 'transparent',
position: 'absolute',
zIndex: 2, // <- zIndex here
flex: 1,
left: 20,
//left: (Dimensions.get('window').width / 2) - 300, // Uncomment this line when test in device.
bottom: 60,
},
talkBubbleSquare: {
width: 300,
height: 120,
backgroundColor: '#2C325D',
borderRadius: 10
},
talkBubbleTriangle: {
position: 'absolute',
bottom: -20,
left: 130,
width: 0,
height: 0,
borderLeftWidth: 20,
borderRightWidth: 20,
borderTopWidth: 20,
borderLeftColor: 'transparent',
borderRightColor: 'transparent',
borderTopColor: '#2C325D',
},
talkBubbleMessage: {
color: '#FFFFFF',
marginTop: 40,
marginLeft: 20,
marginRight: 20,
},
anotherView: {
backgroundColor: '#CCCCCC',
width: 340,
height: 300,
position: 'absolute',
zIndex: 1, // <- zIndex here
},
});
/******************* /CSS STYLES *******************/
更新:我已经移除了Codepen iframe小部件,因为它可能会让一些用户感到困惑,请按照上面的链接查看示例。
const rootTag = document.getElementById('react-root');
之外,所有的代码都是使用React-Native编写的,并且这行代码仅用于Web演示。也许在对答案进行投票前,你应该先仔细阅读它。 - vtisnado
react-native-animatable
库来实现动画效果。 - jose920405