# ReactNative 官方组件 ScrollView 无法直接指定高度的问题

ReactNative 官方组件 ScrollView 无法直接指定高度的问题

图一.png

代码块1:图一容器组件代码

class ... {
   render() {
        const { countryVisa, currentPersonTypeindex } = this.state;
        return (
            <View style={styles.container}>
                <TabHeader
                    list={countryVisa.Material}
                    currentIndex={currentPersonTypeindex}
                    onPressHeaderItem={this._onPressHeaderItem}
                />
                <VisaMaterialList list={countryVisa.Material && countryVisa.Material[currentPersonTypeindex]} />
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: Colors.grayBGColor,
        flex: 1,
    }
})

代码块2: Heder 组件代码

const TabHeader = ({ list, currentIndex, onPressHeaderItem }) => (
        <ScrollView horizontal={true}  contentContainerStyle={styles.contentContainerStyle}>
            {list.map((item, index) => <TabHeaderItem title={item.Cname || item.ApplyName} selected={currentIndex == index} onPress={() => onPressHeaderItem(index)} />)}
        </ScrollView>
)
const styles = StyleSheet.create({
    contentContainerStyle: {
        width: Layout.window.width,
        height: 40,
        backgroundColor: Colors.whiteColor,
        borderBottomWidth: StyleSheet.hairlineWidth,
        borderBottomColor: Colors.gray400,
    }
})

原因: :官方组件 ScrollView 默认会撑满父级容器高度。
解决方案:官方组件需要在 ScrollView 外面嵌套父级组件来约束高度,比如嵌套一个 View 设置 height 为40。

代码块3:图一修改后Heder 组件代码

const TabHeader = ({ list, currentIndex, onPressHeaderItem }) => (
    <View style={styles.container}>
        <ScrollView horizontal={true} contentContainerStyle={styles.contentContainerStyle}>
            {list.map((item, index) => <TabHeaderItem title={item.Cname || item.ApplyName} selected={currentIndex == index} onPress={() => onPressHeaderItem(index)} />)}
        </ScrollView>
    </View>
)
const styles = StyleSheet.create({
    container: {
        height: 40,
    },
    contentContainerStyle: {
        width: Layout.window.width,
        backgroundColor: Colors.whiteColor,
        borderBottomWidth: StyleSheet.hairlineWidth,
        borderBottomColor: Colors.gray400,
    }
})

代码块3 运行结果.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,536评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,975评论 2 59
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • . symptom . sympathy . symphony: 'simfeni
    享悦moonlight阅读 292评论 0 0
  • 我的小学是江汉平原所有普通小学中的一所其貌不扬的学校。它甚至不是楼房,而是树林掩映着的几排低矮的瓦房,没有水泥地面...
    水天一色飞飞阅读 239评论 0 0