简体   繁体   English

React Native - 如何衡量ScrollView中的内容大小?

[英]React Native - How to measure size of content in ScrollView?

I want to measure the size of the content in my ScrollView . 我想测量ScrollView内容的大小。

Therefore, I am using measure from NativeMethodsMixin : 因此,我使用NativeMethodsMixin measure

import NativeMethodsMixin from 'NativeMethodsMixin'

I am not quite sure where to go from here, most SO posts relating to this issue seem to be outdated. 我不太确定从哪里开始,与此问题相关的大多数SO帖子似乎已经过时了。

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 ). 我知道我需要为我的ScrollView创建一个ref (我做了并调用_scrollView ,我可以使用this.refs._scrollView访问它)。

Problem is, I can't just pass in this.refs._scrollView into measure like so: 问题是,我不能像这样将this.refs._scrollView传入measure

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

I am getting the following error then: 我收到以下错误:

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

I then tried to retrieve the actual node handle using findNodeHandle and pass it into measure as discussed in this github issue : 然后,我尝试使用findNodeHandle检索实际的节点句柄,并将其传递给measure ,如此github问题中所述

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

But this results in the same error. 但这会导致同样的错误。 Any ideas? 有任何想法吗?

ScrollView defines a prop called onContentSizeChange : ScrollView定义了一个名为onContentSizeChange的道具:

<ScrollView
  onContentSizeChange={(width, height) => {
    console.log(width, height);
  }}>
  {content}
</ScrollView>
NativeMethodsMixin.measure.call(elementToMeasure, (x, y, width, height) => {
  ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM