0

I'm working on my first react-native app, and am trying to wrap my head around layouts. Here is my code so far:

import React from 'react'
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native'

function App() {
  return (
    <View style={styles.container}>
      <Text style={styles.text}>My App</Text>
      <View style={styles.buttonContainer}>
        <TouchableOpacity>
          <Text>My Button</Text>
        </TouchableOpacity>
      </View>
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    alignItems: 'center',
    justifyContent: 'center'
  },
  text: {
    fontSize: 45
  },
  buttonContainer: {
    backgroundColor: 'gray',
    paddingHorizontal: 20,
    paddingVertical: 5
  }
})

export default App

This renders as:

enter image description here

However, what I was hoping for was to vertically center the text, but push the button to the bottom, just slightly above it. Something like this:

enter image description here

I was able to get that output by adding:

position: 'absolute',
bottom: 50

to my buttonContainer styles, but I feel like this isn't the right way to do it. Is there a proper flex-y way to do this?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
inhwrbp
  • 569
  • 2
  • 4
  • 17
  • You need to add weight for it. First find out ratio what u want to display text and button. Eg: u can think u can divide screen 6 equal parts then 5parts got text and lower 1part show button. U need to add flex:5 attribute to text style and flex:1 attribute to button style for it – PushpikaWan Oct 26 '18 at 01:10
  • https://stackoverflow.com/q/36191516/3597276 – Michael Benjamin Oct 26 '18 at 01:13
  • If you want `My App` to be aligned perfectly in center, consider three flexed `View`s: top and bottom with 20% height and center one with 60% height. Put your button in bottom `View` with `alignItems` property set to `flex-end`. – Uzair A. Oct 26 '18 at 06:47

3 Answers3

0

You need to add weight for it. First find out ratio what u want to display text and button. Eg: u can think u can divide screen 6 equal parts then 5parts got text and lower 1part show button. U need to add flex:5 attribute to text style and flex:1 attribute to button style for it.

You can find more details about flex and other best practices by navigating these links https://medium.com/@drorbiran/the-full-react-native-layout-cheat-sheet-a4147802405c

https://www.tutorialspoint.com/react_native/react_native_flexbox.htm

PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
0

Wrap the text My App with a View and add the flex style to it.

<View style={{flex:1}}>
  <Text style={styles.text}>My App</Text>
</View>

OR

When considering your exact expectation this will solve your issue.

import React from 'react'
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native'

function App() {
return (
<View style={styles.container}>
  <View style={{flex:12}}>
     <Text style={styles.text}>My App</Text>
  </View>
  <View style={styles.buttonContainer}>
    <TouchableOpacity>
      <Text>My Button</Text>
    </TouchableOpacity>
  </View>
</View>
)
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    alignItems: 'center',
    justifyContent: 'center'
  },
  text: {
    fontSize: 45
  },
    buttonContainer: {
    backgroundColor: 'gray',
    paddingHorizontal: 20,
    flex: 1,
    alignItems: 'center',
    paddingVertical: 5
  }
})

export default App

Adjust the flex in My App wrapped <View> and buttonContainer accordingly.

Chathurika Senani
  • 716
  • 2
  • 9
  • 25
0

Try this:

function App() {
    return (
      <View style={styles.container}>
        <View style={styles.topPadder} />
        <View style={styles.textView}>
          <Text style={styles.text}>My App</Text>
        </View>
        <View style={styles.buttonContainer}>
          <TouchableOpacity style={styles.buttonStyle}>
            <Text>My Button</Text>
          </TouchableOpacity>
        </View>
      </View>
      )
}

const styles = StyleSheet.create({
  topPadder: {
    flex: 20,
  },
  textView: {
    flex: 60,
    justifyContent: 'center',
  },
  container: {
    flex: 1,
    backgroundColor: 'white',
    alignItems: 'center',
    justifyContent: 'center'
  },
  text: {
    fontSize: 45
  },
  buttonContainer: {
    justifyContent: 'flex-end',
    flex: 20,
    marginBottom: 10,
  },
  buttonStyle: {
    paddingHorizontal: 20,
    paddingVertical: 5,
    backgroundColor: 'gray',
  },
})

export default App

You can set the marginBottom in buttonContainer according to your need.

Uzair A.
  • 1,586
  • 2
  • 14
  • 30