将宽度和高度设置为React-native模式

TK-*_*-95 29 modal-dialog react-native

我无法使用"style"属性配置模态高度和宽度.有没有其他方法来设置模态高度和宽度?

 <Modal style={{height: 300, width: 300}}
        visible={this.state.isVisible}
        onRequestClose={this.closeModal}>
 </Modal>
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用.

max*_*23_ 92

根据Modal文档,没有style要设置的道具.

您可以尝试在<View>内部设置尺寸<Modal>:

<Modal transparent={true}
       visible={this.state.isVisible}
       onRequestClose={this.closeModal}>
  <View style={{
          flex: 1,
          flexDirection: 'column',
          justifyContent: 'center',
          alignItems: 'center'}}>
    <View style={{
            width: 300,
            height: 300}}>
      ...
    </View>
  </View>
</Modal>
Run Code Online (Sandbox Code Playgroud)

  • 如果您希望模态显示为底层视图"变暗"的不同框,则向外部视图添加具有透明度的背景颜色 - 例如,`backgroundColor:'#00000080'`(白色,50%不透明度)和a纯色和填充内部视图,例如`backgroundColor:'#ff',填充:20` (23认同)
  • 如果要根据设备使用Modal尺寸:`style = {{width:Dimensions.get('window')。width * 0.5,height:Dimensions.get('window')。height * 0.5}} ` (3认同)

Sat*_*han 7

尝试添加组件margin:0的样式<Modal>


Dib*_*der 5

Modal文档中没有提到它,但它有一个 style 属性。

以下内容对我有用。

<Modal
    style={styles.modalContent}
    isVisible={this.state.isVisible}
    onBackdropPress={this.closeModal}
>
    <Component {...componentProps} />
</Modal>

const styles = StyleSheet.create({
    modalContent: {
        justifyContent: 'center',
        alignItems: 'center',
        margin: 0
    },
});
Run Code Online (Sandbox Code Playgroud)