【问题标题】:Position of an ImageBackground with resizeMode='contain' in React NativeResizeMode='contain' 的 ImageBackground 在 React Native 中的位置
【发布时间】:2018-12-10 23:39:16
【问题描述】:


我是 React-native 的新手,我有一个非常简单(至少我这么认为)的问题。

我有一个带有 resizeMode='contain' 的 ImageBackground,现在我想将背景定位到容器的顶部...

<ImageBackground style={styles.imageBackground} source={backgroundImage} resizeMode='contain'> 
... some content 
</ImageBackground>

图像以正确的方式呈现,但问题是它垂直居中,而不是我想让它顶部对齐...

This is an example of the result of my ImageBackground

This is an example of the result with the bottom added to ImageStyle

【问题讨论】:

    标签: react-native


    【解决方案1】:

    ImageBackground 组件中,图像不是垂直居中的,而是绝对定位以填充容器。您可以查看来源here。它出现垂直居中的原因可能是因为 resizeMode contain。要使其顶部对齐,您可以使用 imageStyle 属性的用户,您可以将其设置为如下内容:

    <ImageBackground
        imageStyle={{
            bottom: 40 // Whatever offset you want from the bottom
        }}
    />
    

    【讨论】:

    • 是的,我已经看过源代码,但是这对我没有帮助...但是,通过您的解决方案,我没有得到使图像在顶部对齐的效果...我认为在ImageBackground 的样式上使用{ flex: 1, justifyDirection: 'flex-start' } 会起作用,但它不是......
    • flex 不会工作,因为它是绝对定位的。添加底部偏移时到底发生了什么?您能否检查样式,或者更好地创建一个重现您的问题的代码笔。
    • 底部偏移图像向上移动,但我找不到将图像放在每个设备顶部的值,我错了吗?
    • React Native 提供的默认样式已经包含top: 0left: 0right: 0,所以它应该总是出现在每个设备的顶部。
    • 我添加了 2 个示例图像,有和没有底部偏移
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多