我想从屏幕顶部确定元素的位置。对于其他问题,一种方法是使用react native的.measure属性。
参考问题:React Native: Getting the position of an element
所以我做了这样的事情
const AutoComplete = (props) => {
let parentViewRef = useRef(null);
return (
<View
style={styles.modelOpenViewMain}>
<View ref={parentViewRef}
onLayout={({nativeEvent}) => {
console.log(nativeEvent)
if (parentViewRef) {
parentViewRef.measure((x, y, width, height, pageX, pageY) => {
console.log(x, y, width, height, pageX, pageY);
})
}}}/>
<View style={styles.modelOpenInputView}>
<TextInput
value={value.label}
onChangeText={(text) => onchangeHandler(text)}
style={[{color: defaultColor, borderColor: defaultColor}, styles.defaultTextInputStyle, textInputStyle]}
/>
</View>
</View>
)
}我的parentViewRef控制台给了我这个console.log(parentViewRef)
_nativeTag: 73
_children: []
viewConfig:
uiViewClassName: "RCTView"
Commands: {}
bubblingEventTypes: {topSelect: {…}, topBlur: {…}, topChange: {…}, topEndEditing: {…}, topFocus: {…}, …}
directEventTypes: {topClick: {…}, topContentSizeChange: {…}, topLoadingError: {…}, topLoadingFinish: {…}, topLoadingStart: {…}, …}
validAttributes: {hasTVPreferredFocus: true, focusable: true, nativeBackgroundAndroid: true, nativeForeg 而且里面没有任何函数度量,有人能帮我找出我做错了什么吗?
以防万一:我会在3秒后记录我的parentViewRef
setTimeout(() => {
console.log(parentViewRef)
}, 3000);发布于 2020-01-07 23:24:51
由于您已经在分配onLayout属性,因此可以从那里创建y
onLayout={({ nativeEvent }) => console.log(nativeEvent.layout.y)}.measure (如果您仍然需要它,但我想您不需要了)应该在稍后的某个时间点调用,以响应某个事件,但至少在onLayout被执行一次之后调用
发布于 2020-08-31 18:15:00
使用ref时,需要使用parentViewRef.current访问被引用的对象。在您的例子中是parentViewRef.current.measure。
我也有同样的问题,度量没有定义,所以我不确定这是否解决了你的问题,不幸的是,它没有解决我的问题
https://stackoverflow.com/questions/59630788
复制相似问题