Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an item stick to the bottom using flex in react-native

Suppose this is the layout:

<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>

I want to make the view with the styles of footer to position at the bottom of the screen. I tried giving the alignSelf property to the footer, but instead of positioning at the bottom, it positions it to the right side of the screen. How can I make the footer item stick to the end? Thank you.

like image 533
Karl Avatar asked Aug 11 '16 04:08

Karl


People also ask

How do I align an item to the bottom in React Native?

To move the button to the bottom, we use justifyContent to lay out items in the main axis, with flex-end , which aligns the flex items at the end of the container.

How do you position elements in React Native?

position in React Native is similar to regular CSS, but everything is set to relative by default, so absolute positioning is always relative to the parent. If you want to position a child using specific numbers of logical pixels relative to its parent, set the child to have absolute position.

How do you set absolute position in React Native?

To set an item to have position absolute center with React Native, we can set the position to absolute . Then we set justifyContent and alignItems to center to center out item. to set the position , top , left , right and bottom values. Then we center our View by setting justifyContent and alignItems to center .

Does flexbox work in React Native?

You will normally use a combination of flexDirection , alignItems , and justifyContent to achieve the right layout. Flexbox works the same way in React Native as it does in CSS on the web, with a few exceptions.


5 Answers

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 :)

like image 153
David Avatar answered Oct 18 '22 02:10

David


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

like image 44
Michael Benjamin Avatar answered Oct 18 '22 03:10

Michael Benjamin


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

bottomContainer: {
    flex: 1,
    justifyContent: 'flex-end',
}
like image 33
manuelBetancurt Avatar answered Oct 18 '22 02:10

manuelBetancurt


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

footer: {
    position: 'absolute',
    height: 40,
    left: 0, 
    top: WINDOW_HEIGHT - 40, 
    width: WINDOW_WIDTH,
}
like image 37
CoderLim Avatar answered Oct 18 '22 02:10

CoderLim


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!

like image 11
Shivam Jha Avatar answered Oct 18 '22 04:10

Shivam Jha