当图像的调整模式为“包含”时,它似乎会将实际图像在中心对齐/对齐,但图像内容却对齐/对齐于 flex 的开头。
<Image resizeMode="contain" ...>
<Text>Title</Text>
</Image>
通过以下方式,我看到文本出现在图像上方。
有没有办法将包含的图像垂直对齐到顶部?
当图像的调整模式为“包含”时,它似乎会将实际图像在中心对齐/对齐,但图像内容却对齐/对齐于 flex 的开头。
<Image resizeMode="contain" ...>
<Text>Title</Text>
</Image>
通过以下方式,我看到文本出现在图像上方。
有没有办法将包含的图像垂直对齐到顶部?
我能够使用以下代码模拟CSS的backgroundPosition:
<View style={{ overflow: 'hidden' }}>
<Image src={{ uri: imageURI }} style={{ height: /*adjust_as_needed*/, resizeMode: 'cover' }} />
</View>
因为在视图上使用了overflow: 'hidden',所以可以调整图片的高度而不会看到图片的额外高度。此外,您需要使用'cover'而不是'contain'作为调整模式,否则如果将图片的高度设置得太大,则会得到一个居中的图片,它不如View容器宽。
在下面的两个示例中,顶部示例中的图片高度(蓝色虚线)比底部示例更大,因此图像的中心线位于视图中更低的位置。通过减小第二个示例中的图像高度(蓝色虚线),可以使图像的中心线在视图中向上移动。
resizeMethod="resize"
我找到了一个解决方案,不需要任何额外的标签或技巧,只需要一个单一的属性。
背景
由于我的远程图片@3x普通大小,一旦使用{ height: 100, width: 300, resizeMode: 'contain' }
样式值在手机上加载,它会自动居中显示。
示例
为了解决这个问题,我只需添加以下属性:resizeMethod
:
<Image
style={{ height: 100, width: 300, resizeMode: 'contain' }}
source={{ uri: 'https://www.fillmurray.com/900/300' }}
resizeMode="contain"
resizeMethod="resize"
/>
resizeMode="contain"
是iOS的属性,也应该能够解决问题。 - Zachary Dahanvar SampleApp = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Image source={{uri: "http://placekitten.com/300/505"}} style={styles.image}>
<Text style={styles.instructions}>
Hello !
</Text>
</Image>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'stretch',
backgroundColor: '#F5FCFF',
},
image: {
height: 500,
justifyContent: "space-around", // <-- you can use "center", "flex-start",
resizeMode: "contain", // "flex-end" or "space-between" here
},
instructions: {
textAlign: 'center',
color: 'white',
backgroundColor: "transparent",
fontSize: 25,
},
});
请查看https://rnplay.org/apps/9D5H1Q,获取一个运行的示例。
我需要做同样的事情。我发现实现这个的最好方法是明确给出图像的高度或宽度。然后,通过将您的图像包装在另一个组件中,您可以使用justifyContent来完全控制图像的位置。
确实,您可能并不总是拥有图像的宽度或高度。但是,很多时候(特别是如果使用resizeMode:'contain'),您希望将高度或宽度相对于某些东西设置。我希望高度等于视口的宽度。这是我使用的解决方案:
<View style={{ flex: 1, justifyContent: 'flex-start' }}>
<Image
source={{ uri: imagePath }}
resizeMode="contain"
style={{ height: vw(100) }}
/>
</View>
vw是一个辅助函数,来自this的答案。
这很简单。首先找到屏幕的高度:
const height = Dimensions.get('window').height
现在进入您的样式属性,将marginVertical属性设置为以下值(height / 4):
<Image source = {// some url} resizeMode = 'contain' style = {width: '100%', height: height/2, marginVertical: height/4}
现在您的图像将垂直居中。
我刚刚遇到了同样的问题,但是建议的解决方案都对我无效。一个设置了resizeMode="contain"
的图像总是居中显示,并忽略外部容器的justifyContent
设置。
我通过将图像的width
和height
都设置为undefined
,并指定其aspectRatio
来“修复”此问题:
<View
style={
justifyContent: 'flex-end',
}}
>
<Image
source={require('./img/source.png')}
style={{
// This is as strange as it gets. The `flex-end` setting works _only_ if I pass the image's aspect ratio
// and `undefined` for width and height properties. If any of these are missing, the image with `resizeMode="contain"
// will be centered vertically and ignore the `justifyContent` setting of the outer container.
aspectRatio: 0.5622, // In my case
width: undefined,
height: undefined,
resizeMode: 'contain',
}}
/>
</View>