如何在屏幕中心将 React Native Modal 显示为小窗口

Tal*_*eem 1 react-native react-native-stylesheet

我想在屏幕中间显示我的本机模式,即使我已经完成了样式,但窗口显示在我的屏幕顶部而不是屏幕中心。这是我迄今为止尝试过的:

 <Modal animationType = {"slide"} transparent = {true}
        style={styles.modal}
               visible = {this.state.modalVisible}
               onRequestClose = {this.closeModal}>

               <View style={{justifyContent: 'center', backgroundColor: '#ffff', margin: 0, 
          alignItems: 'center'}}>
                  <Text >Enter Email</Text>
                  <TextInput
              underlineColorAndroid='transparent'
              onChangeText={(email) => this.setState({email})}/>
              <Text>Enter Password</Text>
              <TextInput 
              secureTextEntry={true}
              underlineColorAndroid='transparent'
              onChangeText={(password) => this.setState({password})}/>
                  <TouchableHighlight onPress = {() => {
                     this.toggleModal(!this.state.modalVisible)}}>
                     <Text >Close</Text>
                  </TouchableHighlight>
               </View>
            </Modal>
Run Code Online (Sandbox Code Playgroud)

这是模态样式:

const styles = StyleSheet.create({
      modal:{
          position:"relative",
        width: 250, 
        height: 100,
        backgroundColor: '#FFF',
        justifyContent: 'center',
        alignSelf: 'center',
      }
});
Run Code Online (Sandbox Code Playgroud)

Afr*_*ain 7

styleReactNative 本机中没有道具Modal(请参阅此链接中的文档,稍后将在答案中详细介绍)。

要正确的风格你Modal,你需要创建一个View具有的风格flex: 1为所有的子元素的父。例如,您将执行以下操作:

<Modal
  animationType={"slide"}
  transparent={true}
  visible={this.state.modalVisible}
  onRequestClose={this.closeModal}
>
  <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}> // modalContainerStyle
    <View style={styles.childStyle}>
       {...}
    </View>
  </View>
</Modal>
Run Code Online (Sandbox Code Playgroud)

childStyle您的示例中模态中第一个元素的样式在哪里。

您还可以在上面的代码中添加一个backgroundColorofrgba(0,0,0,0.5)modalContainerStyle使其具有正确的模态外观。

回到style道具,它仅在名为 的 RN Modal 的社区管理包装器中提供react-native-modal。您可以在此处阅读更多相关信息:https : //github.com/react-native-community/react-native-modal