Transparent overlay in React Native
Solution 1:
The key to your ListView not moving down, is to set the positioning of the overlay to absolute
. By doing so, you can set the position and the width/height of the view manually and it doesn't follow the flexbox layout anymore. Check out the following short example. The height of the overlay is fixed to 360, but you can easily animate this or make it dynamic.
'use strict';
var React = require('react-native');
var Dimensions = require('Dimensions');
// We can use this to make the overlay fill the entire width
var { width, height } = Dimensions.get('window');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to the React Native Playground!
</Text>
<View style={[styles.overlay, { height: 360}]} />
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
// Flex to fill, position absolute,
// Fixed left/top, and the width set to the window width
overlay: {
flex: 1,
position: 'absolute',
left: 0,
top: 0,
opacity: 0.5,
backgroundColor: 'black',
width: width
}
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);
module.exports = SampleApp;
Solution 2:
import React, { Component } from 'react';
import { Image, StyleSheet, View } from 'react-native';
export default class App extends Component {
render() {
return (
<Image source={{uri: 'http://i.imgur.com/IGlBYaC.jpg'}} style={s.backgroundImage}>
<View style={s.overlay}/>
</Image>
);
}
}
const s = StyleSheet.create({
backgroundImage: {
flex: 1,
width: null,
height: null,
},
overlay: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
backgroundColor: 'red',
opacity: 0.3
}
});
Live demo: https://sketch.expo.io/S15Lt3vjg
Source repo: https://github.com/Dorian/sketch-reactive-native-apps
Solution 3:
You can use this example for create overlay.You can change state for visible and invisible for overlay.
import React, { Component } from "react";
import { View, Text, StyleSheet } from "react-native";
class Popup extends Component {
constructor(props) {
super(props);
this.state = {
isPopupTrue: true
};
}
render() {
return (
<View style={styles.container}>
{ this.state.isPopupTrue &&
(<View style={styles.overlay}>
<View style={styles.popup}>
<Text style={styles.text}> Overlay </Text>
</View>
</View>)
}
</View>
);
}
}
export const styles = StyleSheet.create({
container: {
flex:1,
backgroundColor: "#c0d6e4"
},
overlay: {
position: "absolute",
top: 0,
right: 0,
bottom: 0,
left: 0,
justifyContent: "center",
alignItems: "center",
backgroundColor: "gray",
opacity: 0.9,
},
text: {
width: "20%",
fontSize: 15,
color: "black",
fontWeight: "bold"
},
});