Reputation: 1249
I have an absolutely positioned round image. The image needs to take only 17% of screen width, and also be positioned 5 pixels from the top.
Problem is, when I resize the image to take up 17% of screen width, it does so but at the same time the container becomes elongated. The image itself does not stretch, but is positioned in the middle of the elongated Image container.
Styles:
const styles = StyleSheet.create({
imageBase: {
position: 'absolute',
left: 15,
top: 5,
width: '17%',
resizeMode: 'contain',
backgroundColor: 'red',
}
});
What I'm rendering:
<Image
style = { styles.imageBase }
source = { require('roundImage.png') }>
</Image>
I need it to either cut off the extra space from the Image container, or position the image at the top of the container, instead of middle.
Upvotes: 0
Views: 200
Reputation: 331
One way to solve this is to wrap your image in a View with an aspect ratio of 1:
<View style={{ flex: 1 }}>
<View
style={{
position: 'absolute',
left: 15,
top: 5,
width: '17%',
aspectRatio: 1,
}}
>
<Image
style={{
width: '100%',
height: '100%',
backgroundColor: 'red',
resizeMode: 'contain',
}}
source={require('roundImage.png')}
/>
</View>
</View>
Upvotes: 1