Nest inside <Image> but behind the image - image

The problem I'm having is that the image inside the image, is on top of the outside image, the outside image is a half transparent image so I want it to be ON TOP of the nested image, as an overlay image, what can I do?
<Image
style={it}
source={require('../../img/Rarities/red.png')}
>
<View>
{
item[2] != null ?
<View style={{ width: 15, height: 15, backgroundColor: item[2].Color }} />
: null
}
<Image
style={{height:'70%',width:'70%'}}
source={{ uri: item[1].base64 }}
/>
</View>
</Image>
I tried:
changing the zIndex of the images didn't work..

In React Native, components are rendered in the order they are defined - therefore it can be tricky to reverse the order and render a parent on top of a child.
Instead, you can render the images as siblings, and use a parent container component with a little position: absolute trickery to get the images to align on top of each other.
For the following view structure:
<View style={styles.imageContainer}>
<Image source={{uri: image1}} style={styles.bottom} />
<Image source={{uri: image2}} style={styles.top} />
</View>
You can achieve this effect with following styles. See inline comments for explanation:
const styles = StyleSheet.create({
// The container controls the top image size
imageContainer: {
width: 200,
height: 200
},
bottom: {
// horizontal margin: (100% -width) / 2
marginHorizontal: '15%',
// vertical margin: (100% - height) / 2
marginVertical: '15%',
width: '70%',
height: '70%'
},
top: {
// positioned absolutely
position: 'absolute',
opacity: 0.5,
// full width/height of imageContainer
width: '100%',
height: '100%'
},
});
You can see it in action in this Snack demo.

I'm not sure that i understand what are you meaning correctly but you can use the Image components like this if you want to have them inside each other:
<View>
<Image
style={{flex:1}}
source={require('./img/favicon.png')}
/>
<Image
style={{height:'30',width:'30', position:'absolute'}}
source={require('./img/favicon.png')}
/>
</View>
Note: the Image sources and the dimensions should be replaced.

Related

React Native - Can't clickTouchableOpacity inside ImageBackground

I want create a rotate icon inside a image:
<ImageBackground style={stylesNative2.image} source={{ uri }} >
<TouchableOpacity onPress={ () => { alert("handler here") }} tyle={styles.rotateImageIcon}>
<Icon name='rotate-ccw' type='Feather' style={styles.rotateImageIcon} />
</TouchableOpacity>
</ImageBackground>
const stylesNative2 = StyleSheet.create({
image: {
zIndex: 0,
position: 'absolute',
height: h,
width: WIDTH,
resizeMode: 'cover',
transform: [{ rotate: this.state.imageRotation + 'deg' }]
}
});
const styles = StylesManager.getStyles({
rotateImageButton: {
backgroundColor: 'transparent',
elevation: 0,
zIndex: 1
},
rotateImageIcon: {
marginTop: '1rem',
marginLeft: '1rem',
fontSize: '1.7rem',
color: 'white',
}
});
The icon appear but the TouchableOpacity is not working.
Any idea why it's not working?
I think it's because ImageBackgroud have a pointerEvent like this one:
pointer-events: none;
http://facebook.github.io/react-native/docs/view#pointerevents
Can you try to wrap your <TouchableOpacity> on a <View>?
Mayby it's the zIndex...
A touchable opacity is transparent so you don't have to add zIndex on it.
When we add TouchableOpacity inside the ImageBackground .
We need to add the zindex greater than ImageBackground.
like zindex:100
Elements with a higher index will be placed on top of elements with a lower index. Note: Z index only works on positioned elements ( position:absolute , position:relative , or position: fixed

When your image is not resizing in RN

I am bringing in an image on my RN app. It's an image of the Simpsons family. When I set the width and height to 100%, the image becomes too big for my simulator. I want it to be centered and formatted to the center of my screen. However, when I adjust the percentages, the image starts getting cutoff (obviously). Another thing, the image itself is not centered despite me stating it in the code (see below). Do any of you have any recommendations? This png image is going on top of a background image already (jpeg).
<Image
source={{
uri:"https://upload.wikimedia.org/wikipedia/en/0/0d/Simpsons_
FamilyPicture.png"
}}
style={{ width: 100 , height: 120, justifyContent: "center", alignContent: "center" }}
/>
Try change resizeMode for contain.
justifyContent and alignContent needs to be style attributes of the container. Something like this:
<View style={{flex: 1, flexDirection: 'column', justifyContent: "center", alignContent: "center"}}>
<Image
source={{
uri:"https://upload.wikimedia.org/wikipedia/en/0/0d/Simpsons_
FamilyPicture.png"
}}
style={{ width: 100 , height: 120}}
/>
</View>

React Native LayoutAnimation scrollView container size

I have some components wrapped in a ScrollView and for some condition the bottom most component shouldn't render. When that happens, I use LayoutAnimation to hide it. The problem is that when the component disappear, the ScrollView jumps to the new content height directly, without any animation at all.
I want to use LayoutAnimation since I have screens where the contents height is unknown.
Example image
If you look at the image, when the button is pressed, the screen will instantly jump to the blue box without any animation.
state = { showGreenBox: false };
renderBottomBox() {
if (this.state.showGreenBox) {
return (
<View style={{ height: 300, width: 100, backgroundColor: 'green' }} />
);
}
}
render() {
return (
<ScrollView>
<View style={{ height: 300, width: 100, backgroundColor: 'red' }} />
<View style={{ height: 300, width: 100, backgroundColor: 'blue' }} />
{this.renderBottomBox()}
<TouchableOpacity
onPress={() => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
this.setState({ showGreenBox: !this.state.showGreenBox });
}}
>
<Text>Press to collapse green box</Text>
</TouchableOpacity>
</ScrollView>
);
}

Why is this error in Image Component of React-Native?

I would like to background black opacity.
I've wrote this code.
<Image source={require('../assets/images/test_img1.png')} style={[s.lastReviewImage]}>
<View style={s.blackOverlay}/>
</Image>
lastReviewImage: {
flex:1,
width: null, height: null
},
blackOverlay: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
backgroundColor: 'red',
opacity: 0.3
},
But, this is give me a error.
Error: The Image component cannot contain children. if you want to render content on top of the image, consider using absolute positioning.
I know what this error means but I've surely set absolute property in blackOverlay.
I've refer this guides.
Text Overlay Image with Darkened Opacity React Native
https://snack.expo.io/S15Lt3vjg
But, It's not working for me with the error.
If you want to set some background color or an image as the Screen Background, you need to use ImageBackground in place of Image.
<ImageBackground
source={yourSourceFile}
style={s.blackOverlay}>
<....yourContent...>
</ImageBackground>

Text stops wrapping when put on a background Image in react-native

For some reason when I put Text inside Image it stops wrapping. I've tried flexWrap: 'wrap' but it doesn't help.
Here is how it looks like
If I put the Text outside of Image than it works OK.
Here is the code:
class wraptest extends Component {
render() {
return (
<View style={styles.container}>
<Image
source={require('./image.jpg')}
style={styles.image}>
<View style={styles.textContainer}>
<Text style={styles.text}>Text goes here.</Text>
</View>
</Image>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
image: {
flex: 1,
resizeMode: 'cover',
justifyContent: 'center',
},
textContainer: {
backgroundColor: 'transparent',
alignItems: 'center',
},
text: {
fontSize: 40,
fontWeight: 'bold'
}
});
I've also pushed full project here https://github.com/OleksandrBezhan/react-native-text-wrap-test
The reason the text wraps when it is outside of the image is because it is a direct child of the "container" style which defines the flex box. Try putting flex: 1 on either the textContainer or text styles.

Resources