react-native - 设置背景图片位置(即 web 中的 `background-position-x` 和 `background-position-y`)

标签 react-native

当我有背景图片时:

<Image style={{flex: 1, width: null, height: null, resizeMode: 'cover'}} source={require('../images/background.png')}>
    <!-- Children -->
</Image>

我希望我的图像覆盖整个屏幕,同时保持图像的纵横比。 (请参阅此 SO question 为什么我使用 width: null, height: null 。)

但是,如果图像宽度远小于高度,则图像会居中,从而裁剪顶部。

问题:如何让图像从顶部开始,很像 background-position: 50% 0会在网络上吗?

最佳答案

嘿:) 尝试增加父容器的高度,即:

款式:

export default StyleSheet.create({
  image: {
    flex: 1,
    height: null,
    width: null,
  },
  imageContainer: {
    bottom: 0,
    flex: 1,
    height: '130%',
    left: 0,
    position: 'absolute',
    right: 0,
    top: 0,
    width: '100%',
  },
})

成分:
<View style={styles.imageContainer}>
        <Image
          resizeMode="cover"
          source={require('src/assets/images/background.jpg')}
          style={styles.image}
        />
      </View>

使用此解决方案,您可以稍微操纵您的图像。

关于react-native - 设置背景图片位置(即 web 中的 `background-position-x` 和 `background-position-y`),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39644074/

相关文章:

javascript - react native 事件侧边栏菜单项

react-native - React-Native ScrollView 与自定义起始位置的中心对齐

javascript - mobx 商店更改后更新选项卡导航器上的徽章

android - 调试 React-Native 应用程序时如何在 Chrome devtools 中查看 React 选项卡?

react-native - 意外的 token ,预期 ";" Jest + react native 中的 enzyme

windows - 在 react-native-push-notification :processReleaseResources 中构建失败

javascript - 无法在 react-native 中使用嵌套的 ScrollView

javascript - react 导航 2 : How to check previous scene and to disable tab change

javascript - React Native - 使用动态名称的图像需求模块

javascript - single & 和 single | 是什么意思运营商在flow js中做什么?