React Native - 获取组件在视图中的位置 [英] React Native - Get the position of a component in the View

查看:68
本文介绍了React Native - 获取组件在视图中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取组件在 React Native 中的位置.

I'm trying to get the position of a component in react native.

说我有一个观点:

<ScrollView>
    lots of items...
    lots of items...
    lots of items...
    <View></View>
</ScrollView>

最后我希望能够最终滚动到视图所在的位置.

In the end I want to be able to eventually scroll to where the View is.

此链接显示了如何使用本机 ui 管理器获取位置,但是在运行时返回:

This link shows how to get the position using the native ui manager, however when ran it returns:

尝试测量布局,但偏移量或尺寸为 NaN"

"Attempted to measure layout but offset or dimensions were NaN"

React Native:获取元素的位置

我尝试过

this.refs.VIEW.measure((ox, oy, width, height, px, py) => {
    //ox ... py all = 0
});

推荐答案

有时,如果您在 componentDidMount 中使用 measure,则必须将其包装在 setTimeout 中:

Sometimes if you're using measure in componentDidMount you have to wrap it in a setTimeout:

setTimeout(() => {
    this.refs.VIEW.measure((ox, oy, width, height, px, py) => {
    });
}, 0);

这篇关于React Native - 获取组件在视图中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆