如何在反应原生模态中调暗背景?

Asb*_*Ali 9 reactjs react-native

以下是我创建的反应原生模态,仍然无法找到如何调暗背景和透明模式周围的透明.我没有使用任何外部库并试图找到没有库的解决方案.这是可以做到这一点?

在此输入图像描述

我的模态组件

render() {
let modal = this.state.modalType=='skill'? <SkillModal /> : <TrialModal />;
return (
  <View style={styles.container}>
    <Modal
      animationType='slide'
      onRequestClose={() => console.log('no warning')}
      presentationStyle="FormSheet"
      transparent
      visible={this.state.showModal}
    >
      <View>
        <View style={styles.modalView} >
          <TouchableOpacity
            onPress={this.closeModalFunc}
          >
            <Text style={styles.closeText}>X</Text>
          </TouchableOpacity>
          <View>
            {modal}
          </View>
        </View>
      </View>
    </Modal>
  </View>
  );
}
Run Code Online (Sandbox Code Playgroud)

模态组件样式

import {StyleSheet} from 'react-native';
import colors from '../../../theme/colors';
import metrics from '../../../theme/metrics';
import {container} from '../../../theme/base';

const styles = StyleSheet.create({
container: {
 backgroundColor: colors.background.gray,
},
modalView: {
 backgroundColor: colors.background.white,
 margin: 40,
},
closeText: {
  backgroundColor: colors.background.purpleishBlue,
  color: colors.background.white,
  borderRadius: metrics.avatar.small,
  width: 32,
  padding: 6,
  alignSelf: 'flex-end',
  textAlign: 'center',
  borderWidth: 1,
  borderColor: colors.background.white,
 },
});

export default styles;
Run Code Online (Sandbox Code Playgroud)

mai*_*anh 23

我通过创建我自己的组件MyPopup来解决这个问题,如下所示

class MyPopup extends React.PureComponent {

render() {
    return (
        <Modal 
            animationType="fade"
            transparent={true}
            visible={this.props.visible}
            >
                <View style={{flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: 'rgba(0,0,0,0.5)'}}>
                    {this.props.children}
                </View>
        </Modal>
    )
}}
Run Code Online (Sandbox Code Playgroud)

然后我就像使用它一样

<MyPopup visible={this.state.modalVisible}>
<View style={{
    width: '90%',
    height: 50,
    borderColor: '#ccc',
    borderWidth: 1,
    borderStyle: 'solid',
    backgroundColor: 'white',
    elevation: 20,
    padding: 10,
    borderRadius: 4,
}}>
    <Text>Hello, This is my model with dim background color</Text>
</View>
Run Code Online (Sandbox Code Playgroud)

  • 嗨,但模态不覆盖状态栏。你有解决方案吗? (4认同)

小智 17

我和你有同样的问题,我通过transparent={true}在模态的道具中设置并在模态的主视图的样式中设置 50% 的透明度来解决它:backgroundColor: 'rgba(0, 0, 0, 0.5)'

  • 这是一个很好的答案,但请记住,如果您有背景,它不适用于手机的标题。 (3认同)
  • 我认为这是最简洁明了的方法。它不影响其他组件,逻辑也很简单。 (2认同)

v.n*_*ahc 13

我设法获得了一个透明的深色背景,当您单击它时它会关闭模式。
我在模态元素中使用第一个外部容器,它具有全屏尺寸。其中,内部容器具有盒子大小并位于屏幕中央。

这里是代码(react:v16.13,react-native:v0.63.2)

import React from 'react';
import {Modal, Text, TouchableWithoutFeedback, View} from 'react-native';
import {style} from './style';

const MyModal = ({visible, onDismiss}) => {
  return (
    <Modal visible={visible} transparent>
      <TouchableWithoutFeedback onPress={() => onDismiss()}>
        <View style={style.modal}>
          <TouchableWithoutFeedback>
            <View style={style.modalInner}>
              <Text>Something in the modal</Text>
            </View>
          </TouchableWithoutFeedback>
        </View>
      </TouchableWithoutFeedback>
    </Modal>
  );
};

export default MyModal;
Run Code Online (Sandbox Code Playgroud)

这是一个回调,切换从其父级赋予该组件onDismiss的变量值。visible

TouchableWithoutFeedbackonPress在容器上添加事件处理。外部响应点击,关闭/关闭模式。内部的不执行任何操作(onPress未给出时的默认行为)。

风格:

import {StyleSheet} from 'react-native';

export const style = StyleSheet.create({
  modal: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'rgba(0,0,0,0.5)',
  },

  modalInner: {
    height: 200,
    padding: 35,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: '#FFF',
    shadowColor: '#000',
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25,
    shadowRadius: 3.84,
    elevation: 5,
  },
});
Run Code Online (Sandbox Code Playgroud)

请注意, my 的父元素<MyModal>是我的应用程序的根<View>,样式为{flex: 1}


May*_*ime 9

使用react-native-modal,您可以使用backgroundOpacity属性在模态可见时使背景变暗

import React, {useState} from 'react';
import {Button, Text, View} from 'react-native';
import Modal from 'react-native-modal';

function ModalTester() {
  const [isModalVisible, setModalVisible] = useState(false);

  const toggleModal = () => {
    setModalVisible(!isModalVisible);
  };

  render() {
return (
  <View style={{flex: 1}}>
    <Button title="Show modal" onPress={toggleModal} />

    <Modal isVisible={isModalVisible} backdropOpacity={0.3}>
      <View style={{flex: 1}}>
        <Text>Hello!</Text>

        <Button title="Hide modal" onPress={toggleModal} />
      </View>
    </Modal>
  </View>
);
  }
}

export default ModalTester;
Run Code Online (Sandbox Code Playgroud)


Ahs*_*Ali 6

您可以通过编程方式设置可见View时主体的不透明度Modal

<View style={[styles.container, this.state.showModal ? {backgroundColor: 'rgba(0,0,0,0.5)'} : '']}>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答,我已经尝试过了。但这没有意义,因为模型的背景是父组件。 (2认同)

Sha*_*Ali 6

我只是将整个 modalView 包装在另一个视图中,然后简单地对其应用背景

<View style={styles.modalbackdrop}>   
   <View style={styles.modalView}></View>
</View>
Run Code Online (Sandbox Code Playgroud)

并应用以下

backgroundColor: 'rgba(0, 0, 0, 0.8)',
height: '100%',
Run Code Online (Sandbox Code Playgroud)


Tah*_*lid 5

我创建了一个容器样式,然后应用了 rgba 值,backgroundColor例如:

modalContainer:{
    flex: 1,
    //backgroundColor: 'transparent',
    backgroundColor: 'rgba(0,0,0,0.7)',
    alignItems: 'center',
    justifyContent: 'center',
},
Run Code Online (Sandbox Code Playgroud)

然后在模态中,我创建了带圆角的实际模态对话框(我通过在模态内容中添加另一个元素并给它一个带圆角的白色背景来实现这一点)。