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

2024-01-02

我想测量我的内容的大小ScrollView.

因此,我正在使用measure from NativeMethodsMixin:

import NativeMethodsMixin from 'NativeMethodsMixin'

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

我明白我需要创建一个ref to my ScrollView(我这样做并称之为_scrollView,我可以使用它访问它this.refs._scrollView).

问题是,我不能直接通过this.refs._scrollView into measure像这样:

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问题 https://github.com/facebook/react-native/issues/1060:

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

但这会导致同样的错误。有任何想法吗?


ScrollView 定义了一个名为内容大小改变 https://facebook.github.io/react-native/docs/scrollview.html#oncontentsizechange:

<ScrollView
  onContentSizeChange={(width, height) => {
    console.log(width, height);
  }}>
  {content}
</ScrollView>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Native - 如何测量 ScrollView 中内容的大小? 的相关文章

随机推荐