在标题顶部显示自定义弹出警报覆盖-React Navigation

时间:2018-06-25 23:26:41

标签: react-native

我试图在React Navigation的Header组件的顶部显示此自定义模式弹出窗口。参见下图:

enter image description here

我尝试在覆盖和父视图容器上设置zIndexes,但标头在组件堆栈中是单独的。有人知道如何解决吗?我尝试将BlurView放在标头组件上。

import React, { Component } from "react";
import PropTypes from "prop-types";
import { StyleSheet, View, Animated, Text, TextInput } from "react-native";

import BlockButton from "../common/BlockButton";

import { BlurView } from "react-native-blur";

export default class BlurAlert extends Component {
    static navigationOptions = ({ navigation }) => {
        return {
            headerStyle: {}
        };
    };

    constructor(props) {
        super(props);

        this.state = {
            visible: this.props.visible,
            title: null,
            body: null
        };
    }

    render() {
        if (this.props.visible) {
            return (
                <View style={styles.wrapper}>
                    <BlurView style={styles.blurView} blurType="dark" blurAmount={5} />

                    <View style={styles.box}>
                        <Text style={styles.titleText}>{this.props.title}</Text>
                        <Text style={styles.bodyText}>{this.props.body}</Text>

                        {this.props.children}

                        <BlockButton
                            backgroundColor={"#F0AC8F"}
                            borderColor={"#F0AC8F"}
                            fontColor={"white"}
                            title={this.props.buttonText}
                            padding={23}
                            marginTop={25}
                            disabled={false}
                            onPress={this.props.onClosed}
                        />
                    </View>
                </View>
            );
        } else {
            return <View />;
        }
    }
}

BlurAlert.propTypes = {
    title: String,
    body: String,
    buttonText: String,
    visible: Boolean
};

BlurAlert.defaultProps = {
    visible: false,
    buttonText: "OK"
};

const styles = StyleSheet.create({
    wrapper: {
        position: "absolute",
        top: 0,
        left: 0,
        bottom: 0,
        right: 0,
        height: "100%",
        width: "100%",
        zIndex: 9999
    },
    blurView: {
        flex: 1,
        zIndex: 99999
    },
    container: {
        margin: 25,
        zIndex: 99999,
        position: "relative",
        top: 0,
        left: 0,
        bottom: 0,
        right: 0
    },
    box: {
        backgroundColor: "white",
        padding: 20,
        position: "absolute",
        top: "15%",
        borderColor: "black",
        borderWidth: StyleSheet.hairlineWidth,
        margin: 25,
        alignItems: "center",
        shadowColor: "#000",
        shadowOffset: { width: 0, height: 1 },
        shadowOpacity: 0.1,
        shadowRadius: 3,
        zIndex: 99999,
        width: "85%"
    },
    titleText: {
        fontFamily: "Lora",
        fontSize: 20
    },
    bodyText: {
        marginTop: 15,
        textAlign: "center",
        fontFamily: "Lora",
        fontSize: 16,
        lineHeight: 20
    }
});

1 个答案:

答案 0 :(得分:0)

您可以尝试使用“模态”:

import React, { Component } from "react";
import PropTypes from "prop-types";
import { StyleSheet, View, Animated, Text, TextInput, Modal } from "react-native";

import BlockButton from "../common/BlockButton";

import { BlurView } from "react-native-blur";

export default class BlurAlert extends Component {
    static navigationOptions = ({ navigation }) => {
        return {
            headerStyle: {}
        };
    };

    constructor(props) {
        super(props);

        this.state = {
            visible: this.props.visible,
            title: null,
            body: null
        };
    }

    render() {
        if (this.props.visible) {
            return (
                  <Modal
                    transparent
                    animationType="fade"
                    visible={this.state.visible}
                    onRequestClose={() => {
                      console.log('Modal has been closed.');
                    }}>
                  >
                  <View style={styles.wrapper}>
                      <BlurView style={styles.blurView} blurType="dark" blurAmount={5} />

                      <View style={styles.box}>
                          <Text style={styles.titleText}>{this.props.title}</Text>
                          <Text style={styles.bodyText}>{this.props.body}</Text>

                          {this.props.children}

                          <BlockButton
                              backgroundColor={"#F0AC8F"}
                              borderColor={"#F0AC8F"}
                              fontColor={"white"}
                              title={this.props.buttonText}
                              padding={23}
                              marginTop={25}
                              disabled={false}
                              onPress={this.props.onClosed}
                          />
                      </View>
                  </View>
                </Modal>
            );
        } else {
            return <View />;
        }
    }
}

BlurAlert.propTypes = {
    title: String,
    body: String,
    buttonText: String,
    visible: Boolean
};

BlurAlert.defaultProps = {
    visible: false,
    buttonText: "OK"
};

const styles = StyleSheet.create({
    wrapper: {
        position: "absolute",
        top: 0,
        left: 0,
        bottom: 0,
        right: 0,
        height: "100%",
        width: "100%",
        zIndex: 9999
    },
    blurView: {
        flex: 1,
        zIndex: 99999
    },
    container: {
        margin: 25,
        zIndex: 99999,
        position: "relative",
        top: 0,
        left: 0,
        bottom: 0,
        right: 0
    },
    box: {
        backgroundColor: "white",
        padding: 20,
        position: "absolute",
        top: "15%",
        borderColor: "black",
        borderWidth: StyleSheet.hairlineWidth,
        margin: 25,
        alignItems: "center",
        shadowColor: "#000",
        shadowOffset: { width: 0, height: 1 },
        shadowOpacity: 0.1,
        shadowRadius: 3,
        zIndex: 99999,
        width: "85%"
    },
    titleText: {
        fontFamily: "Lora",
        fontSize: 20
    },
    bodyText: {
        marginTop: 15,
        textAlign: "center",
        fontFamily: "Lora",
        fontSize: 16,
        lineHeight: 20
    }
});