Mr. Robot
Mr. Robot

Reputation: 1824

How to vertically and horizontally align child components 'on top' of parent in React Native?

I'm having a lot of trouble getting two child components to vertically and horizontally align 'on top' of their parent element in React Native.

I am trying to place Loader and Button on top of RTCView.

I currently have this JSX returning in the parent:

  if (localStream) {
    return (
      <View style={styles.videoViewWrapper}>
        <RTCView style={styles.android} streamURL={localStream.toURL()} />
        <View
          style={{ justifyContent: 'center', position: 'absolute', }}
        >
          <Loader
            title={callDetails}
          >
          </Loader>
          <Button
            mode="contained"
            style={{ width: 150, margin: 100 }}
            onPress={handleCancelCall}>
            Cancel
          </Button>
        </View>
      </View>
    );
  }

And this inside Loader:

Loader = props => {
  return (
    <>
      <StatusBar
        hidden
      />
      <View style={{
        flex: 1,
        alignItems: 'center',
      }}>
        <Text
          style={styles.text}
        >
          {props.title}
        </Text>
        <ProgressBar color={Colors.green800} indeterminate={true} style={{ width: 100, height: 1 }} />
      </View>
    </>
  )
}

const styles = StyleSheet.create({
  text: {
    fontFamily: "Quicksand-Light",
    fontSize: 22,
    textAlign: "center",
    color: "#333333",
    marginBottom: 25,
    justifyContent: 'center',
  }
});

Whilst I have achieved getting Loader to display 'on top' of it's parent, I cannot move them to their position at the top of the screen. enter image description here

Any help would be much appreciated!

Upvotes: 2

Views: 3484

Answers (1)

aytek
aytek

Reputation: 1942

Your absolute positioned View needs to cover its parent. You can achieve this by adding top: 0, left: 0, right: 0, bottom: 0 to its style

You may need to edit your Button style. I removed it from your code, add it if you need margin or a specific width.

<View style={{ justifyContent: 'center', alignItems: 'center', position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }}>
   <Loader title={callDetails} />
   <Button mode="contained" onPress={handleCancelCall}>Cancel</Button>
</View>

Upvotes: 3

Related Questions