React Native: vertical centering when using ScrollView

前端 未结 6 527
孤街浪徒
孤街浪徒 2021-01-30 12:28

I\'m using React Native and I\'m having trouble retaining vertical centering of elements as soon as I introduce a ScrollView. To demonstrate, I created 3 apps with React Native

6条回答
  •  孤街浪徒
    2021-01-30 13:07

    EDIT: You can now use .

    For older version of React Native that do not support flexGrow, use the solution below.


    The only way I have found to reliably do this for both iOS and Android is to set minHeight of the inner view to the size of the ScrollView. E.g:

     this.setState({minHeight: event.nativeEvent.layout.height})}
    >
    

    Note: I have in-lined the styles and functions above for simplicity but you will probably want to use constant references for the unchanging values and memorise the changing style to prevent unnecessary rerenders.

    const {minHeight} = this.state;
    // Manually memorise changing style or use something like reselect...
    if (this.lastMinHeight != minHeight) {
        this.lastMinHeight = minHeight;
        this.contentContainerStyle = {minHeight: minHeight}
    }
    
    

提交回复
热议问题