1

我試圖讓我的內容從React Native的頂部開始100px。我曾嘗試與在React Native的ScrollView中設置contentInset和contentOffset

const OFFSET = 100 
const ScrollViewTest = (props) => (
    <ScrollView 
    contentInset={{ top: OFFSET }} 
    contentOffset={{ y: OFFSET }} 
    > 
    <Text>Test</Text> 
    </ScrollView> 
) 

但是,當我呈現在屏幕上,它從0開始像素,但如果我滾動了一下,它會從頂部滾動爲100px,呆在那裏。

因此,看起來React Native不會在初始化時觸發contentOffsetcontentInset屬性。

我該如何解決這個問題?我也嘗試過設置automaticallyAdjustContentInsets={false},但沒有任何更改。

此外,它似乎這些屬性僅適用於iOS。 Android有沒有類似的屬性?

回答

0

填充般的效果:

const OFFSET = 100 
const ScrollViewTest = (props) => (
    <ScrollView> 
     <View style={{ height: OFFSET }} /> 
     <Text>Test</Text> 
    </ScrollView> 
) 

頭般的效果:

const OFFSET = 100 
const ScrollViewTest = (props) => (
    <View style={{ paddingTop: OFFSET }}> 
     <ScrollView> 
      <Text>Test</Text> 
     </ScrollView> 
    </View > 
) 
+0

但是,如果,代替' ..'具有''用'RefreshControl' ,刷新按鈕將在填充狀效果之前,而不是在列表之上,所以我不能完成相同的操作: – Jamgreen

+0

有趣的溢出:「隱藏」將在這個案例 –

0
componentDidMount(){ 
     if(Platform.OS==='ios'){ 
      this.refs._scrollView.scrollToOffset({animated:false,offset:-OFFSET}); 
     } 
    } 

    render(){ 
     return(
      <ScrollView 
       ref="_scrollView" 
       contentInset={{top: OFFSET}} 
      ...> 
       ... 
      </ScrollView> 
     ) 
    } 
相關問題