ScrollView使用{位置:绝对}捕捉到适当位置

Jea*_*san 7 react-native

由于表单上显示的图层很多,并且要创建透明效果,因此需要将ScrollView放置在绝对位置上。不幸的是,当添加{position:'absolute'}样式时,松开手指后,ScrollView会回到顶部。我没有阅读有关stackoverflow的所有相关线程。

以下是以下代码的屏幕截图:http : //imgur.com/a/fd4ad

这是我正在使用的代码:import React,'react'中的{Component}; 从'react-native'导入{View,ScrollView,Text};

class HomeTest extends Component {
    render() {
        const { headerTextStyle, homeView, scrollViewStyle, textStyle } = styles;

        return (
            <View>
                <ScrollView style={scrollViewStyle} contentContainerStyle={homeView}>
                    <Text style={textStyle}>I'd love to figure out why this is not working.........................</Text>
                </ScrollView>
                <Text style={headerTextStyle}>Header</Text> 
            </View>
        );
    }
}

const styles = {
    headerTextStyle: {
        fontSize: 40,
        alignSelf: 'center'
    },
    scrollViewStyle: {
        position: 'absolute',
        paddingTop: 60,
        marginTop: 0 
    },
    homeView: {
        alignItems: 'center',
        justifyContent: 'center'
    },
    textStyle: {
        fontSize: 96
    },
};

export default HomeTest;
Run Code Online (Sandbox Code Playgroud)

Jea*_*san 6

在GitHub上找到的解决方案:https : //github.com/facebook/react-native/issues/5438

好的,秘诀是添加以下样式组件:(我必须弄清楚原因-稍后再发布-并提高我的CSS技能)

  • 将样式添加到父视图(位置:相对,flex:1)
  • 将新的样式属性添加到滚动视图(顶部,底部,左侧,右侧)

这是更新的代码:

import React, { Component } from 'react';
import { View, ScrollView, Text } from 'react-native';

class HomeTest extends Component {
    render() {
        const { headerTextStyle, homeView, scrollViewStyle, textStyle, mainView } = styles;

        return (
            <View style={mainView}>
                <ScrollView style={scrollViewStyle} contentContainerStyle={homeView}>
                    <Text style={textStyle}>I'd love to figure out why this is not working.........................</Text>
                </ScrollView>
                <Text style={headerTextStyle}>Header</Text> 
            </View>
        );
    }
}

const styles = {
    headerTextStyle: {
        fontSize: 40,
        alignSelf: 'center'
    },
    scrollViewStyle: {
        position: 'absolute',
        top: 0,
        bottom: 0,
        left: 0,
        right: 0,
        paddingTop: 60
    },
    homeView: {
        alignItems: 'center',
        justifyContent: 'center'
    },
    textStyle: {
        fontSize: 96
    },
    mainView: {
        flex: 1,
        position: 'relative'
    }
};

export default HomeTest;
Run Code Online (Sandbox Code Playgroud)


Mah*_*our 5

只需要指定scrollView的高度即可

scrollViewStyle: {
    position: 'absolute',
    paddingTop: 60,
    marginTop: 0 ,
    height: 300 //    <----
},
Run Code Online (Sandbox Code Playgroud)