2017-02-16 83 views
0

有什麼方法可以獲得React Native中整個ScrollView元素的高度?如何用很多子元素獲得ScrollView的高度?

return (
    <View ref='container' style={[ styles.container, backgroundColor, {width: this.props.width} ]}> 
    <ScrollView ref='scroll' style={styles.scrollView}> --> what height with inner content? 
     {this._getTitle()} 
     <View style={styles.content}> 
     {items} 
     </View> 
     <View style={[styles.empty]} /> 
    </ScrollView> 
    </View> 
) 
+0

你問的高度或內容大小的滾動視圖?高度通常是固定的,隨着孩子被添加到滾動容器中,內容大小也會改變。 – Artal

+0

我想要接收scrollView的高度並將其內容添加到@Artal – Dmitry

+0

,高度是固定的。例如:您的滾動視圖可佔用整個屏幕。但內容不一樣,可能會更低,或者可能更高(此時您需要滾動內容才能完成)。請清楚,如果你想檢查高度或內容大小,這是兩件不同的事情。 – Artal

回答

1

我不知道這是一個正式記錄的API,但可以使用本機滾動視圖管理器來獲取內容大小。

添加必要的進口:在你的代碼

import ReactNative, {NativeModules} from 'react-native'; 
const ScrollViewManager = NativeModules.ScrollViewManager; 

呼叫這在一個點上,你已經用上了滾動裁判:

if(ScrollViewManager && ScrollViewManager.getContentSize) { 
    ScrollViewManager.getContentSize(ReactNative.findNodeHandle(this.scrollViewRef), (contentSize) => { 
    console.log(contentSize); 
    }) 
} 
+0

謝謝,它有幫助 – Dmitry