英文:
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 '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",
},
});
I have tried different overflow options, one solution I have is setting the height to Dimensions.get('window').height;
but this has the drawback of not staying updated when the size changes.
Any help would be great, thanks!
答案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('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",
},
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论