使一个项目坚持底部使用反应原生的 Flex

假设这是布局:

<View style={styles.container}>
<View style={styles.titleWrapper}>
...
...
</View>
<View style={styles.inputWrapper}>
...
...
</View>


<View style={styles.footer}>
<TouchableOpacity>
<View style={styles.nextBtn}>
<Text style={styles.nextBtnText}>Next</Text>
</View>
</TouchableOpacity>
</View>
</View>

我想使用页脚样式的视图定位在屏幕的底部。我尝试将 alignSelf属性赋予页脚,但是它没有将其定位在底部,而是将其定位在屏幕的右侧。我怎样才能使页脚项目坚持到底?谢谢你。

156144 次浏览

In React Native, the default value of flexDirection is column (unlike in CSS, where it is row).

Hence, in flexDirection: 'column' the cross-axis is horizontal and alignSelf works left/right.

To pin your footer to the bottom, apply justifyContent: 'space-between' to the container

Absolutely position is another way to fix footer, just like:

footer: {
position: 'absolute',
height: 40,
left: 0,
top: WINDOW_HEIGHT - 40,
width: WINDOW_WIDTH,
}

I would use the following approach:

<View style={styles.container}>


<View style={styles.contentContainer}> {/* <- Add this */}


<View style={styles.titleWrapper}>
...
</View>
<View style={styles.inputWrapper}>
...
</View>


</View>


<View style={styles.footer}>
...
</View>
</View>
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
titleWrapper: {


},
inputWrapper: {


},
contentContainer: {
flex: 1 // pushes the footer to the end of the screen
},
footer: {
height: 100
}
});

This way the styles of titleWrapper and inputWrapper can be updated without breaking the layout of your app and the components themselves are easier to re-use :)

embed other content in a scrollview

<View style={styles.container}>


<ScrollView> {/* <- Add this */}
<View style={styles.titleWrapper}>
...
</View>
<View style={styles.inputWrapper}>
...
</View>
</ScrollView>


<View style={styles.footer}>
...
</View>
</View>

To do this you can use the Stylesheet element position: 'absolute'.

/*This is an Example to Align a View at the Bottom of Screen in React Native */
import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={styles.containerMain}>
<Text> Main Content Here</Text>
<View style={styles.bottomView}>
<Text style={styles.textStyle}>Bottom View</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
containerMain: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
bottomView: {
width: '100%',
height: 50,
backgroundColor: '#EE5407',
justifyContent: 'center',
alignItems: 'center',
position: 'absolute', //Here is the trick
bottom: 0, //Here is the trick
},
textStyle: {
color: '#fff',
fontSize: 18,
},
});

enter image description here

In react native, there are some properties like position: 'absolute', bottom: 0, which you will want to give to your button view

Consider a screen structure

<View style={styles.container}>
<View style={styles.body}> ... </View>
<View style={styles.footer}>...</View>
</View>

You can do it cleanly using Flexbox approach utilizing flex-grow.

const Styles = StyleSheet.create({
container:{
flexDirection: 'column', // inner items will be added vertically
flexGrow: 1,            // all the available vertical space will be occupied by it
justifyContent: 'space-between' // will create the gutter between body and footer
},
})

enter image description here

Note: In case of nested elements, you have to ensure that the parent container has enough height to work with when using flexGrow. Set backgroundColor on parents and child to debug.

for me the answer was to create a container view for the elements, then for the style.

bottomContainer: {
flex: 1,
justifyContent: 'flex-end',
}
import React from 'react'
import { View, StyleSheet } from 'react-native'
function moveToBottom(component) {
return (
<View style={styles.container}>
{component}
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-end',
marginBottom: 36
}
})
export default moveToBottom

Now in our screen, we just need to import:

import moveToBottom from 'library/utils/moveToBottom'

and wrap our button:

{
moveToBottom(
<ImageButton
style={styles.button}
title={strings.onboarding.welcome.button}
onPress={() => {
this.props.navigation.navigate('Term')
}} />
)
}

I tested it and I approve it's the best option to respect the layout without having fixed things to bottom, which is not possible if you use react-native-web in addition of react-native, because people resize and elements overlap on each over.

Source: https://medium.com/react-native-training/position-element-at-the-bottom-of-the-screen-using-flexbox-in-react-native-a00b3790ca42

I have a case in which I have to show a image in the bottom like this, as you can see the sky-blue image is not poped-up with keyboard.

enter image description here

so for this I have created a functional component for image in bottom.

import React, { useEffect, useState } from "react";
import { Keyboard, View, Image } from "react-native";


export const BottomImage = (props) => {


const [shouldShow, showImage] = useState(true);


useEffect(() => {
Keyboard.addListener("keyboardDidShow", _keyboardDidShow);
Keyboard.addListener("keyboardDidHide", _keyboardDidHide);


return () => {
Keyboard.removeListener("keyboardDidShow", _keyboardDidShow);
Keyboard.removeListener("keyboardDidHide", _keyboardDidHide);
};
}, []);


let _keyboardDidShow = () => {
showImage(false)
}


let _keyboardDidHide = () => {
showImage(true)
}




return (<ViewToRender show={shouldShow} src={props.image} />)
}


function ViewToRender(props) {
return props.show ? <Image style=\{\{ position: 'absolute', bottom: 0 }} source={props.src} /> : <View />
}

and to use this Bottom image you have to pass your image to it like :

<BottomImage image={AppImage.signupbottom} />

You can use this style:

row: {
flexDirection: 'row',
height: 50,
justifyContent: 'center',
alignItems: 'center',
position: 'absolute', //Here is the trick
bottom: 0,
}

To fix a View to the bottom, simply use: marginTop: 'auto' .

This worked for me after searching like an hour on the net. I tried experimenting and it worked!

This can be a bit tricky given that parent components can still affect the height of children with 'absolute' styles, I also tried doing "bottom: 0, height: 'auto'" like with normal HTML/CSS, but it didn't work out well, down the line I'll probably create a general component which makes sure the view can fit into the screen size. End result of view with contents

<View> component parameters

style=\{\{
position: 'absolute',
left: 0,
padding: ContainerPadding,
top: TopOffset,
width: ScreenWidth
}}


onLayout={(event) => {
var {x, y, width, height} = event.nativeEvent.layout; // get the View's dimensions after 1st render
SetTopOffset(ScreenHeight - height - HeaderHeight); // Set 'top' to: screen size - height (of view) - parent top offset (optional if no parent offset)
}}

With useState:

const [TopOffset, SetTopOffset] = useState<number>(0); // Controls 'top' of screen
  • HeaderHeight is the height which is added to all my page components, you can remove this variable if you do not have any top spacing. (currently set to 64 default and this variable is updated based on device)
  • ScreenWidth & ScreenHeight are calculated here: export const ScreenWidth = Dimensions.get('screen').width; export const ScreenHeight = Dimensions.get('screen').height;
  • ContainerPadding is a general padding number used across my project (currently set to 12.5)

Quick example in essence, based on @David's answer:

<View style=\{\{flex: 1}}>
<View style=\{\{flex: 1}}>
<Text>Main content</Text>
</View>
<Text>Footer</Text>
</View>