React Native - 如何衡量ScrollView中的内容大小? [英] React Native - How to measure size of content in ScrollView?

查看:107
本文介绍了React Native - 如何衡量ScrollView中的内容大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量 ScrollView 中内容的大小。

I want to measure the size of the content in my ScrollView.

因此,我正在使用衡量来自 NativeMethodsMixin

Therefore, I am using measure from NativeMethodsMixin:

import NativeMethodsMixin from 'NativeMethodsMixin'

我不太确定从哪里开始,大多数与此问题相关的SO帖子似乎已经过时了。

I am not quite sure where to go from here, most SO posts relating to this issue seem to be outdated.

我知道我需要为我创建一个 ref ScrollView (我做过并调用 _scrollView ,我可以使用 this.refs访问它。 _scrollView )。

I understand that I need to create a ref to my ScrollView (which I did and called _scrollView, I can access it using this.refs._scrollView).

问题是,我不能只传入 this.refs._scrollView 进入度量,如下所示:

Problem is, I can't just pass in this.refs._scrollView into measure like so:

NativeMethodsMixin.measure(this.refs._scrollView, (data) => {
  console.log('measure: ', data)
})

我收到以下错误:

ExceptionsManager.js:61 findNodeHandle(...): Argument is not a component (type: object, keys: measure,measureInWindow,measureLayout,setNativeProps,focus,blur,componentWillMount,componentWillReceiveProps)

然后我尝试使用 findNodeHandle 检索实际节点句柄并将其传递给 measure 如本 github问题中所述:

I then tried to retrieve the actual node handle using findNodeHandle and pass it into measure as discussed in this github issue:

const handle = React.findNodeHandle(this.refs._scrollView)
NativeMethodsMixin.measure(handle, (data) => {
  console.log('measure: ', data)
})

但这会导致同样的错误。有什么想法?

But this results in the same error. Any ideas?

推荐答案

NativeMethodsMixin.measure.call(elementToMeasure, (x, y, width, height) => {
  ...
});

这篇关于React Native - 如何衡量ScrollView中的内容大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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