我在Flexbox中使用React-Native路由器。最后,我想展示FlatList的全屏显示和粘性页脚。我尝试flexGrow: 1
为FlatList和flexShrink: 0
View页脚指定,但是它不起作用。我正在Expo中构建应用程序,并在iPhone上查看结果。
import React, {Component} from 'react';
import {
Text,
View,
FlatList,
} from 'react-native'
import {Provider} from 'react-redux'
import {NativeRouter, Route, Link} from 'react-router-native'
export default class App extends Component {
render() {
return <NativeRouter>
<View style={{
display: "flex",
flexDirection: "column",
padding: 10,
}}>
<View style={{
display: "flex",
flexDirection: 'row',
justifyContent: 'space-around'
}}>
<Link
to="/lobby"
underlayColor='#f0f4f7'
style={{
flex: 8,
alignItems: 'center',
padding: 10,
}}>
<Text>Lobby</Text>
</Link>
<Link
to="/"
underlayColor='#f0f4f7'
style={{
flex: 2,
alignItems: 'center',
padding: 10,
}}>
<Text>Home</Text>
</Link>
</View>
<Route exact path="/" component={Home}/>
<Route path="/lobby" component={Lobby}/>
</View>
</NativeRouter>
}
}
const Home = () => (
<Text>
Home
</Text>
);
class Lobby extends Component {
render() {
return <View style={{
flexGrow: 1,
flexDirection: "column",
// justifyContent: "space-between"
}}>
<FlatList
style={{
flexGrow: 1,
}}
data={[
{key: 'Spring Game'},
{key: 'Summer Game'},
{key: 'Fall Game'},
{key: 'Winter Game'},
]}
renderItem={({item}) => <Text style={{
display: "flex",
flex: 1,
padding: 10,
fontSize: 18,
height: 44,
alignContent: "center"
}}>{item.key}</Text>}
/>
<View style={{
display: "flex",
flexDirection: "row",
backgroundColor: "#848441",
flexShrink: 0,
}}>
<Text style={{
flex: 6,
alignSelf: "flex-end"
}}>Footer</Text>
</View>
</View>
}
}
首先,祝您好运:)如果您的“粘滞逻辑”不像“只是始终停留在底部”那么简单,您将必须开始聆听滚动并调整大小...
无论如何,我需要为我的react应用程序使用一个Sticky组件,这就是它对我有效的方式。它的反应方式有点不同,但是就CSS而言应该没问题。
首先,将其父元素设置为具有position: "relative"
,这将使其子元素完全基于父元素定位。此父元素应环绕所有可用区域。因此,您父级View组件的样式几乎可以,请添加(flexBasis: 0
如果尚未占据所有可用区域的话),然后添加position: "relative"
。
将Sticky组件本身(子视图)设置为display: "block"
,具有position: "absolute"
和设置overflow
为可见。从技术上讲,它已经很粘。然后添加bottom: 0
到您的粘滞中,您的组件应该已经粘在底部-现在您只需要使用所需的任何属性或工具来管理其宽度和水平位置。
我想只有Flexbox的规则来解决这个问题,但似乎接近,这是最好的方式position="relative"
和position="absolute"
您还应该注意,此后,便笺盒真正有趣的部分开始了,因为基于便笺的实际位置,您可能必须添加一些js来处理特殊情况。例子:我想现在粘性框将覆盖您的内容。应该在页面底部添加其他空格,以便您实际上可以向下滚动到足以看到内容底部的位置。
补充说明:我知道您是否将display: "flex"
和结合在一起position: "relative"
,以一种特殊的方式进行处理。不确定到底有什么不同,但是在分配position =“ relative”时,block和flex的行为有所不同。如果您在布局中看到怪异,请尝试在Flex视图和块粘滞之间添加一个包装器(块/相对包装器)以规范行为。
我的课程简化了,这是为了粘贴侧边栏:
.StickyBox {
overflow: auto;
position: absolute;
height: inherit;
}
.sidebarWrapper {
flex: 1;
position: relative;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句