Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Center element horizontally in React Native

I want to center a button horizontally, this is my code:

  return (
    <View style={styles.root}>
      <View style={styles.container}>
        <SafeAreaView style={HEADER}>
          <SliderBox
            style={{ height: '100%' }}
            currentImageEmitter={index => setCurrentSliderNumber(index)}
            images={data.images}>
          </SliderBox>
        </SafeAreaView>
        {currentSliderNumber == data.images.length - 1 ? <View style={styles.item}>
          <Button
            style={styles.getStartedButton}
            appearance="ghost"
            status="control"
            onPress={onGetStartedPressed}
          >
            Get Started
          </Button>
        </View> : null}
      </View>
    </View>
  )

and this is my styling:

const styles = StyleSheet.create({
  forgotPasswordButton: {
    paddingHorizontal: 0,
  },
  root: {
    flex: 1,
    height: '100%',
    backgroundColor: '#fff'
  },
  container: {
    borderColor: 'rgba(0,0,0,0.2)',
  },
  getStartedButton: {
    marginVertical: 12,
    backgroundColor: 'red'
  },
  item: {
    borderColor: 'rgba(0,0,0,0.2)',
    position: 'absolute',
    top: '80%',
    left: '50%'
  }, text: {
    position: 'absolute',
    top: '30%',
    left: '10%'
  }
})

I also tried to add alingnItems:center to root:

root: {
        flex: 1,
        height: '100%',
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
      }

But the result is not the one expected:

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try changing the item style object to this:

item: {
    borderColor: 'rgba(0,0,0,0.2)',
    position: 'absolute',
    top: '80%',
    left:0,
    width:"100%",
    alignItems:"center",
    justifyContent:"center"
  },
about 4 years ago · Juan Pablo Isaza Relatório

0

Do not use position until it's necessary you should use flex properties.

return (
    <View style={styles.root}>
      <View style={styles.container}>
        <SafeAreaView style={HEADER}>
          <SliderBox
            style={{ height: '100%' }}
            currentImageEmitter={index => setCurrentSliderNumber(index)}
            images={data.images}>
          </SliderBox>
        </SafeAreaView>
        {currentSliderNumber == data.images.length - 1 ? <View style={styles.item}>
          <Button
            style={styles.getStartedButton}
            appearance="ghost"
            status="control"
            onPress={onGetStartedPressed}
          >
            Get Started
          </Button>
   

 </View> : null}
      </View>
    </View>
  )

Styles

const styles = StyleSheet.create({
  forgotPasswordButton: {
    paddingHorizontal: 0,
  },
  root: {
    flex: 1,
    height: '100%',
    backgroundColor: '#fff'
  },
  container: {
    borderColor: 'rgba(0,0,0,0.2)',
    flexGrow:1,
    justifyContent: "space-between"
  },
  getStartedButton: {
    marginVertical: 12,
    backgroundColor: 'red',
    alignSelf:"center",
    flexGrow:2,
  },
  item: {
    borderColor: 'rgba(0,0,0,0.2)',
    flexGrow:1,
    justifyContent:"center",
    alignItem:"center",
  }, text: {
  }
})

Check out the layout docs

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda