React-native: how do I have a scrollable section of my app, but lock the outer container to the size of the screen?

huangapple go评论54阅读模式
英文:

React-native: how do I have a scrollable section of my app, but lock the outer container to the size of the screen?

问题

我想要一个内部容器,其中包含大量可滚动的内容,我希望外部部分保持在屏幕底部。然而,整个页面都会滚动,内部部分不会滚动,这意味着底部的条可能不会一直可见。

这是我项目的一个简化版本,重现了我遇到的问题:

import { StyleSheet, View, ScrollView } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <ScrollView style={styles.topHalf}>
        <View style={styles.innerScrollable}></View>
      </ScrollView>
      <View style={styles.bottomHalf}></View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    width: "100%",
    height: "100%",
  },
  topHalf: {
    flex: 1,
    overflow: "scroll",
  },
  bottomHalf: {
    height: 100,
    backgroundColor: "#fff",
  },
  innerScrollable: {
    height: 2000,
    backgroundColor: "#000",
  },
});

我尝试了不同的溢出选项,一个解决方案是将高度设置为Dimensions.get('window').height;,但这有一个缺点,即当大小发生更改时不会更新。

任何帮助都将是很有帮助的,谢谢!

英文:

I would like an inside container which holds a large amount of scrolling content, I want an outer section to stay at the bottom of the screen. Instead, the entire page scrolls and the inner section does not, meaning that the bar at the bottom is not always visible.

Here is a simpler version of my project which recreates the issue I am having

import { StyleSheet, View, ScrollView } from &#39;react-native&#39;;

export default function App() {
  return (
    &lt;View style={styles.container}&gt;
        &lt;ScrollView style={styles.topHalf}&gt;
            &lt;View style={styles.innerScrollable}&gt;&lt;/View&gt;
        &lt;/ScrollView&gt;
        &lt;View style={styles.bottomHalf}&gt;&lt;/View&gt;
    &lt;/View&gt;
  );
}

const styles = StyleSheet.create({
  container: {
    width: &quot;100%&quot;,
    height: &quot;100%&quot;,
  },
  topHalf: {
    flex: 1,
    overflow: &quot;scroll&quot;,
  },
  bottomHalf: {
    height: 100,
    backgroundColor: &quot;#fff&quot;,
  },
  innerScrollable: {
    height: 2000,
    backgroundColor: &quot;#000&quot;,
  },
});

I have tried different overflow options, one solution I have is setting the height to Dimensions.get(&#39;window&#39;).height; but this has the drawback of not staying updated when the size changes.

Any help would be great, thanks!

simplified project highlighting issue

答案1

得分: 0

solved with useWindowDimensions().height hook which gives the same value as Dimensions.get('window').height but updates automatically

import { StyleSheet, View, ScrollView, useWindowDimensions } from 'react-native';

export default function App() {
  return (
    <View style={[styles.container, {height: useWindowDimensions().height}]}>
        <ScrollView style={styles.topHalf}>
            <View style={styles.innerScrollable}></View>
        </ScrollView>
        <View style={styles.bottomHalf}></View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    width: "100%",
  },
  topHalf: {
    overflow: "scroll",
  },
  bottomHalf: {
    height: 100,
    backgroundColor: "#fff",
  },
  innerScrollable: {
    height: 2000,
    backgroundColor: "#000",
  },
});
英文:

solved with useWindowDimensions().height hook which gives the same value as Dimensions.get(&#39;window&#39;).height but updates automatically

import { StyleSheet, View, ScrollView, useWindowDimensions } from &#39;react-native&#39;;

export default function App() {
  return (
    &lt;View style={[styles.container, {height: useWindowDimensions().height}]}&gt;
        &lt;ScrollView style={styles.topHalf}&gt;
            &lt;View style={styles.innerScrollable}&gt;&lt;/View&gt;
        &lt;/ScrollView&gt;
        &lt;View style={styles.bottomHalf}&gt;&lt;/View&gt;
    &lt;/View&gt;
  );
}

const styles = StyleSheet.create({
  container: {
    width: &quot;100%&quot;,
  },
  topHalf: {
    overflow: &quot;scroll&quot;,
  },
  bottomHalf: {
    height: 100,
    backgroundColor: &quot;#fff&quot;,
  },
  innerScrollable: {
    height: 2000,
    backgroundColor: &quot;#000&quot;,
  },
});

huangapple
  • 本文由 发表于 2023年6月26日 05:24:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定