Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place top right corner react native

The parent component has the following style

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#FFF',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

I am placing another component within this component. I want it in the top-right corner.

Naturally, I would go with the following style in this child component:

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
    top: 20,
    right: 5,
  },
});

However, it places it in the top left corner. The only way I can move it into the right corner is replacing right: 5 with left: 500. But thats kind of not the way to go...

like image 824
Stophface Avatar asked Aug 16 '17 09:08

Stophface


People also ask

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.


1 Answers

Please check the below stylesheet for child component:

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
    alignSelf: 'flex-end',
    marginTop: -5,
    position: 'absolute', // add if dont work with above
  }
});
like image 123
Jigar Shah Avatar answered Oct 05 '22 18:10

Jigar Shah