react-native Modal with SafeAreaView-wrapper not working
Asked Answered
T

3

21

We have a FilterComponent which renders a Modal, but on iPhone X it's Header is in the Statusbar.

I tried to render it with SafeAreaView but seems like this is not working:

return (
  <SafeAreaView>
    <Modal
      { ...defaultModalProps }
      onRequestClose={ close }
      style={ styles.container }
      visible={ visible }
    >
      <ModalNavbar close={ close }>
        Filter
      </ModalNavbar>
      <View style={ styles.content }>
        ...
      </View>
    </Modal>
  </SafeAreaView>
);

When FilterModal is openend on iPhoneX it still is in the Statusbar and you cant click on anything.

Any idea how to solve this?

thanks.

Typescript answered 31/1, 2018 at 9:7 Comment(0)
P
47

Put SafeAreaView inside the Modal component

return (
  <Modal
    {...defaultModalProps}
    onRequestClose={close}
    style={styles.container}
    visible={visible}
  >
    <SafeAreaView style={{ flex: 1, backgroundColor: "transparent" }}>
      <ModalNavbar close={close}>Filter</ModalNavbar>
      <View style={styles.content}>...</View>
    </SafeAreaView>
  </Modal>
);
Parulis answered 31/1, 2018 at 9:25 Comment(4)
This fixed my issue but I am confused why. Wouldn't it make more sense to have the modal inside the SafeAreaView?Malley
@Malley no, a Modal view will cover the entire screen. It is in no way bound by the size of its caller (parent)Advertisement
looks like StatusBar will not be visible if backgroundColor is not transparent. Am I right?Brahmaputra
@Advertisement , thank you for that explanation. I was so confused onto why the modal was expanding past the safe area.Cheongsam
S
19

if you use react-native-safe-area-context and you have a problem with modal then

      <Modal>
        <SafeAreaProvider>
          <SafeAreaView>
            
             // your modal content

          </SafeAreaView>
        </SafeAreaProvider>
      </Modal>
Slack answered 20/12, 2022 at 9:53 Comment(1)
Thanks! This was the issue for me!Marguerite
P
1

A Modal fill the entire screen, so you need to provide extra spacing inside the Modal. Margin / Padding will not effect on Modal if applied on parent of Modal.

<Modal {...}>
  <TouchableWithoutFeedback onPress={closeModal}>
    <SafeAreaView {...}>
      {...}
    </SafeAreaView>
  </TouchableWithoutFeedback>
</Modal>
Parasitic answered 31/1, 2018 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.