【问题标题】:React Native - Vertical align image with resizeMode "contain"React Native - 使用resizeMode“包含”垂直对齐图像
【发布时间】:2021-11-25 03:14:58
【问题描述】:

当图像具有“包含”调整大小模式时,它似乎在中心对齐/对齐实际图像,但图像内容在 flex 开始时对齐/对齐。

<Image resizeMode="contain" ...>
<Text>Title</Text>
</Image>

通过以下内容,我看到图片上方出现了文字。

有什么方法可以将包含的图像垂直对齐到顶部?

【问题讨论】:

  • 你有没有想过这个问题?你的帖子是我在网上找到的关于这个问题的唯一参考。感谢您分享您拥有的任何其他信息。
  • 对我来说同样的问题。但是如果我用其他组件替换 Image 就可以了。为什么Image如此特别?
  • 我认为 Kyle 正在寻找的效果是通过 CSS 属性 background-position (developer.mozilla.org/en-US/docs/Web/CSS/background-position) 在 Web 上实现的。我也在寻求解决这个问题。使用封面或包含时,我并不总是希望图像在视图中垂直和水平居中。
  • fyi,react native 不支持背景位置:/

标签: react-native


【解决方案1】:

我能够使用以下代码模拟 CSS backgroundPosition:

<View style={{ overflow: 'hidden' }}>
  <Image src={{ uri: imageURI }} style={{ height: /*adjust_as_needed*/, resizeMode: 'cover' }} />
</View>

由于 View 上的 overflow: 'hidden' 可以调整图像的高度,而不会看到图像的额外高度。对于调整大小模式,您还需要使用“覆盖”而不是“包含”,否则如果您将图像的高度设置得太大,您最终会得到一个不像 View 容器那么宽的居中图像.

在下面的顶部示例中,图像高度(蓝色虚线)大于底部示例,因此图像的中心线在视图中较低。通过在第二个示例中降低图像的高度(蓝色虚线),图像的中心线在视图中向上移动。

【讨论】:

    【解决方案2】:

    使用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"
    />
    

    【讨论】:

    • 不适用于百分比高度/宽度,仅适用于硬编码值。
    • AFAIK,当我尝试这样做时,它根本无法处理百分比,将垂直对齐问题放在一边。
    • @ZacharyDahan 似乎只是一个 Android 方法:reactnative.dev/docs/image#resizemethod-android
    【解决方案3】:

    您需要在图像上使用样式来设置所需的垂直对齐方式。

    var 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 获取运行示例

    【讨论】:

    • 您好,感谢您的回复。不幸的是,这并不能完全解决我面临的问题。我在rnplay.org/apps/9qKrYA 添加了一个示例。请注意,即使使用 flex-start 实际图像仍然在中心。 prntscr.com/8wc3cf
    • 你到底想达到什么目的?你能用油漆或其他东西画它吗?你想让你的图像在行的顶部(垂直)在它们下面有空白吗?
    【解决方案4】:

    我需要做同样的事情。我发现完成此操作的最佳方法是明确给出图像的高度或宽度。然后,通过将您的图像包装在其他组件中,您可以使用 justifyContent 来完全控制图像的位置。

    确实,您可能并不总是拥有图像的宽度或高度。然而,很多时候(尤其是使用 resizeMode: 'contain')你想要设置相对于某物的高度或宽度。我希望高度等于视口的宽度。这是我使用的解决方案:

    <View style={{ flex: 1, justifyContent: 'flex-start' }}>
        <Image
          source={{ uri: imagePath }}
          resizeMode="contain"
          style={{ height: vw(100) }}
        />
     </View>
    

    vw 是一个辅助函数,来自this 答案。

    【讨论】:

      【解决方案5】:

      它非常简单。首先找到屏幕的高度:

      const height = Dimensions.get('window').height
      

      现在转到您的样式属性并将 ma​​rginVertical 属性设置为 (height / 4):

      <Image source = {// some url} resizeMode = 'contain' style = {width: '100%', height: height/2, marginVertical: height/4}
      

      现在您的图像将垂直居中。

      【讨论】:

        【解决方案6】:

        我刚遇到同样的问题,建议的解决方案都不适合我。带有resizeMode="contain" 设置的图像始终居中并忽略外部容器的justifyContent 设置。

        我设法通过将图像的widthheight 设置为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>
        

        【讨论】:

          【解决方案7】:

          您需要修复一些值而不设置其他值。在大多数情况下,让宽度固定。小心使用父弹性选项。它可能会改变你的设计。

          【讨论】:

            猜你喜欢
            • 2016-07-01
            • 2018-09-13
            • 1970-01-01
            • 1970-01-01
            • 2010-12-03
            • 2022-12-23
            • 2021-05-01
            相关资源
            最近更新 更多