React native,ScrollView的子项不会填满整个高度

agm*_*eod 17 flexbox react-native

所以我在视图的顶部有一个水平滚动视图.ScrollView包含具有指定宽度的节点.然后我在ScrollView的底部有一个边框,就像你可以在这个屏幕上看到的那样:http://i.imgur.com/pOV1JFP.png

如您所见,顶部ScrollView的子节点未到达边框.但是,如果我将ScrollView更改为View with flexDirection: 'row',则子节点将填充高度.我尝试在scrollview上更改一些属性,例如:

  • 设置padding:0contentContainerStyle
  • automaticallyAdjustContentInsets={false}
  • contentInsets直接改变值

似乎没有人解决这个问题.

scrollview代码和样式:

var styles = StyleSheet.create({
  nav: {
    padding: 0,
    marginTop: 30,
    flex: 1,
    borderBottomWidth: 1,
    borderColor: '#000000'
  }
});

<ScrollView
  style={[{width: screen.width}, styles.nav]}
  horizontal={true}
  showsHorizontalScrollIndicator={true}
  automaticallyAdjustContentInsets={false}>
  {dayNavItems}
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

子组件(组成dayNavItems)

const styles = StyleSheet.create({
  container: {
    paddingLeft: 15,
    paddingRight: 15,
    width: 50,
    justifyContent: 'center'
  },
  odd: {
    backgroundColor: '#ccc'
  },
  selected: {
    backgroundColor: '#39b54a'
  },
  text: {
    fontFamily: 'Optima'
  }
});

class DayNavItem extends React.Component {
  static propTypes = {
    day: React.PropTypes.object.isRequired,
    odd: React.PropTypes.bool,
    selectDay: React.PropTypes.func.isRequired,
    selected: React.PropTypes.bool
  };

  render() {
    const d = new Date(this.props.day.created_at);
    const viewStyles = [styles.container];
    if (this.props.selected) {
      viewStyles.push(styles.selected);
    } else if (this.props.odd) {
      viewStyles.push(styles.odd);
    }
    return (
      <TouchableOpacity style={viewStyles} onPress={() => this.props.selectDay(this.props.day.uuid)}>
        <View>
          <Text style={styles.text}>{getMonth(d)}</Text>
          <Text style={styles.text}>{d.getDate()}</Text>
        </View>
      </TouchableOpacity>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Rap*_*nga 25

添加即可解决问题contentContainerStyle={{flexGrow: 1}}

<ScrollView contentContainerStyle={{flexGrow: 1}}>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

  • 这应该被接受的答案。+ 您还应该将子视图设置为 flex:1 。高度将被填充并且滚动也将起作用。 (10认同)

Mal*_*ing 8

有一个叫财产contentContainerStyleScrollView。我只是解决了类似的问题,我设置flex: 1ScrollViewstylesScrollViewcontentContainerStyle,和子View组件。

  • 滚动以这种方式停止在Android上运行。 (3认同)

Fel*_*ger 7

其他答案是正确的,但只是为了提供一个澄清的例子:

<ScrollView contentContainerStyle={{ flex: 1 }}>
  {children}
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,在 contentContainerStyle 上使用“flex: 1”会在内容超出 ScrollView 高度的情况下中断滚动。(您将无法在 ScrollView 中滚动)您将只想*仅*在 contentContainerStyle 上使用 `flexGrow: 1`。资料来源:我刚刚在我的应用程序中执行了此操作,上面 Raphael 的答案解决了该问题。 (8认同)

Alb*_*lvo 6

我总是最终在我的所有项目中创建这个组件:

import * as React from 'react'
import { ScrollView, ScrollViewProps, StyleSheet } from 'react-native'

export function FullHeightScrollView(
  props: {
    children: React.ReactNode
  } & Omit<ScrollViewProps, 'contentContainerStyle'>
) {
  return (
    <ScrollView contentContainerStyle={styles.grow} {...props}>
      {props.children}
    </ScrollView>
  )
}

const styles = StyleSheet.create({
  grow: { flexGrow: 1 },
})
Run Code Online (Sandbox Code Playgroud)

使用它代替 React Native 的ScrollView.


Sta*_*ano 5

设置flex: 1ScrollView 的contentContainerStyle属性对我有用。