reactjs - How to make a View Parent Component resize to fit child image component
问题描述
Having trouble making a parent view resize to just fit its child components one of which is an Image component that will change its width to match the screen width and then the height should simply be whatever necessary to conserve aspect ratio of the image.
cardContainer
refers to the Card that will house mulitple components and should resize height in order to fit components. postMedia
refers to styling for an image that will fill the width of the screen but the height will be unknown (want this to be done dynamically to conserve aspect ratio without distortion or cropping. mediaContainer
is the container for the image which will resize according to the necessary height to fit the image and is one of several components inside the Card.
const styles = StyleSheet.create({
cardContainer: {
backgroundColor: 'grey',
width: '100%',
height: undefined,
borderTopWidth: 2,
borderBottomWidth: 2,
borderTopColor: 'black',
flex: 0,
flexShrink: 1
},
postMedia: {
resizeMode: 'contain',
width: '100%',
height: undefined
},
mediaContainer: {
backgroundColor: 'green',
flex: 0,
flexShrink: 1
}
});
I've tried setting the child components to flex: 1 but that results in the views filling the entire screen which I don't want. I only want to have the cardContainer
just large enough to fit all content without stretching the entire screen.
The code that works for just one image that I'm working with is below:
const styles = StyleSheet.create({
cardContainer: {
backgroundColor: 'grey',
width: '100%',
height: undefined,
borderTopWidth: 2,
borderBottomWidth: 2,
borderTopColor: 'black',
flex: 0,
flexShrink: 1
},
postMedia: {
resizeMode: 'contain',
width: '100%',
height: 233
},
mediaContainer: {
backgroundColor: 'green',
flex: 0,
flexShrink: 1
}
});
I just need to find a way for the view to use the height that fits the image instead of hard coding as above.
解决方案
react-native-scalable-image
library solved my issue. The library dynamically finds the height to conserve the aspect ration of image.
推荐阅读
- css - 试图选择一个没有它自己的类的特定链接
- sql-server - 连接记录
- javascript - 如果其 url 更改,如何收听弹出窗口的“消息”?
- amazon-web-services - ElasticSearch 备份到 S3 AWS
- ruby - 在 Ruby 中通过 TCP 发送消息或文件的代码
- python - Keras - 如何处理不同长度的波输入,将训练数据分成批次
- mysql - 如何在mysql中获取具有相同ID的数据
- typescript - Angular Unit Test SpyOn 给我错误
- c# - 如何在 LINQ C# 中加入三个表时进行分组
- authentication - StackOverflow 是否使用 OpenID Connect 进行 Google 登录?