Search code examples
react-nativeexposcrollview

scrollview height does not work correctly but why?


I have a horizontal scroll view. I set the height to 100. But If my finger is at the bottom of my app then I can also scroll. So I want only to scroll if the user is on the height of 100. Now I can scroll wherever I want in the screen.

  return (
    <KeyboardAwareScrollView style={{margin: 0, padding: 0}}>
      <View style={{height: height - 150, backgroundColor: '#fff'}}>
        <Text style={styles.title}>Storys</Text>
        <ScrollView
        horizontal={true}
        contentContainerStyle={{height: 100, marginLeft: 12, backgroundColor: 'red'}}
        scrollEventThrottle={50}
        showsHorizontalScrollIndicator={false}
        decelerationRate="normal"
        >
        {
          storys.length > 0 
          ?
            storys.map((el, i) => {
              return (
                <TouchableOpacity key={`index-${i}`} style={{padding: 0, marginRight: 16, borderWidth: 2, justifyContent: 'center', alignItems: 'center', borderRadius: 100, borderColor: 'red', height: 68, width: 68}}>
                  <Image resizeMode="contain" source={{ uri: `http://xxxx:3000/uploads/${el}`}} style={{height: 55, width: 55, borderRadius: 500}} />
                </TouchableOpacity>
              )
            })
          :
          <Text>Es sind zurzeit keine Storys vorhanden.</Text>
        }
        </ScrollView>
      </View>
      <View style={{flex: 1, paddingLeft: 16, paddingRight: 16, paddingBottom: 12, backgroundColor: '#fff', alignItems: alignItem, justifyContent:'space-between', flexDirection: 'row'}}>
        <TouchableOpacity onPress={() => setAddStory(true)}>
          <AntDesign name="pluscircleo" size={32} color="#444" />
        </TouchableOpacity>
        <TextInput
          onChangeText={e => setChatMessage(e)} 
          onFocus={() => setAlignItem('flex-end')}
          onEndEditing={() => handleAlign()}
          value={chatMessage}
          multiline={true}
          style={[styles.input, {height: Math.max(50, areaHeight)}]}
          onContentSizeChange={e => {
            setAreaHeight(e.nativeEvent.contentSize.height);
          }}
          placeholder="Gebe eine Nachricht ein..." />
        <TouchableOpacity style={{padding: 8, paddingRight: 10, borderRadius: 8, backgroundColor: 'red'}}>
          <Ionicons name="paper-plane-outline" size={28} color="#fff" />
        </TouchableOpacity>
      </View>
    </KeyboardAwareScrollView>

Solution

  • you can do this by wrapping the ScrollView into a View with height of 100 the code bellow will help you:

    return (
        <KeyboardAwareScrollView style={{ margin: 0, padding: 0 }}>
            <View style={{ height: height - 150, backgroundColor: "#fff" }}>
              <Text style={styles.title}>Storys</Text>
              <View style={{ height: 100 }}>
                <ScrollView
                  horizontal={true}
                  contentContainerStyle={{
                    height: 100,
                    marginLeft: 12,
                    backgroundColor: "red"
                  }}
                  scrollEventThrottle={50}
                  showsHorizontalScrollIndicator={false}
                  decelerationRate="normal"
                >
                  {storys.length > 0 ? (
                    storys.map((el, i) => {
                      return (
                        <TouchableOpacity
                          key={`index-${i}`}
                          style={{
                            padding: 0,
                            marginRight: 16,
                            borderWidth: 2,
                            justifyContent: "center",
                            alignItems: "center",
                            borderRadius: 100,
                            borderColor: "red",
                            height: 68,
                            width: 68
                          }}
                        >
                          <Image
                            resizeMode="contain"
                            source={{ uri: `http://xxxx:3000/uploads/${el}` }}
                            style={{ height: 55, width: 55, borderRadius: 500 }}
                          />
                        </TouchableOpacity>
                      );
                    })
                  ) : (
                    <Text>Es sind zurzeit keine Storys vorhanden.</Text>
                  )}
                </ScrollView>
              </View>
            </View>
            <View
              style={{
                flex: 1,
                paddingLeft: 16,
                paddingRight: 16,
                paddingBottom: 12,
                backgroundColor: "#fff",
                alignItems: alignItem,
                justifyContent: "space-between",
                flexDirection: "row"
              }}
            >
              <TouchableOpacity onPress={() => setAddStory(true)}>
                <AntDesign name="pluscircleo" size={32} color="#444" />
              </TouchableOpacity>
              <TextInput
                onChangeText={e => setChatMessage(e)}
                onFocus={() => setAlignItem("flex-end")}
                onEndEditing={() => handleAlign()}
                value={chatMessage}
                multiline={true}
                style={[styles.input, { height: Math.max(50, areaHeight) }]}
                onContentSizeChange={e => {
                  setAreaHeight(e.nativeEvent.contentSize.height);
                }}
                placeholder="Gebe eine Nachricht ein..."
              />
              <TouchableOpacity
                style={{
                  padding: 8,
                  paddingRight: 10,
                  borderRadius: 8,
                  backgroundColor: "red"
                }}
              >
                <Ionicons name="paper-plane-outline" size={28} color="#fff" />
              </TouchableOpacity>
            </View>
          </KeyboardAwareScrollView>