簡體   English   中英

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

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

我想測量ScrollView內容的大小。

因此,我使用NativeMethodsMixin measure

import NativeMethodsMixin from 'NativeMethodsMixin'

我不太確定從哪里開始,與此問題相關的大多數SO帖子似乎已經過時了。

我知道我需要為我的ScrollView創建一個ref (我做了並調用_scrollView ,我可以使用this.refs._scrollView訪問它)。

問題是,我不能像這樣將this.refs._scrollView傳入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問題中所述

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

但這會導致同樣的錯誤。 有任何想法嗎?

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