我想从屏幕顶部确定元素的位置。从其他问题来看,一种方法是使用 react native 的 .measure 属性?
参考问题:React Native:获取元素的位置
所以我做了这样的事情
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>
)
}
Run Code Online (Sandbox Code Playgroud)
我的 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
Run Code Online (Sandbox Code Playgroud)
那里没有任何功能度量,有人可以帮我弄清楚我做错了什么吗?
以防万一:我在 3 秒后记录我的 parentViewRef
setTimeout(() => {
console.log(parentViewRef)
}, 3000);
Run Code Online (Sandbox Code Playgroud)
由于您onLayout已经分配了道具,因此您可以y从那里获得
onLayout={({ nativeEvent }) => console.log(nativeEvent.layout.y)}
Run Code Online (Sandbox Code Playgroud)
.measure(如果您仍然需要它,但我想您不需要)应该在稍后的某个时间点调用以响应某些事件,但在 onLayout 至少执行一次之后
| 归档时间: |
|
| 查看次数: |
1390 次 |
| 最近记录: |