如何在React Native Expo中将图像适应父容器的边界

huangapple go评论84阅读模式
英文:

How to fit image inside the bounds of a parent container in React Native Expo

问题

我一直在尝试将一个标志图像放入登录页面的父容器中。页面的格式如下:

<NativeBaseProvider>
    <View style={styles.container}>
        <View style={styles.logoBox}> 
            {/* <Image source={images.logo} style={styles.logo} alt="Company Logo"  /> */}
            {/* <NativeBaseProvider> */}
                <Image  
                    source={images.logo}
                    alt="Company Logo" 
                    style={styles.logo}
                />
            {/* </NativeBaseProvider> */}
        </View>
        ... 其他登录页面内容
    </View>
</NativeBaseProvider>
container: {
    flex: 1,
    // width: '100%',
    flexDirection: "column",
    alignItems: 'center',
    justifyContent: 'center',
},
logoBox: {
    // height: '100%',
    height: 300,
    width: '80%',
    // maxWidth: '100%',
    alignItems: 'center',
    justifyContent: 'center',
    // padding: SIZES.medium,
    marginBottom: 60,
},
logo: {
    width: undefined,
    height: undefined,
    resizeMode: 'contain',
    flex: 1,
    // maxWidth: '100%',
    // objectFit: 'contain',
},

我遇到的问题是图像非常大。在网页视图中,我可以使其适应logoBox父容器。但是当我在移动应用视图中查看时,图像会溢出屏幕两侧。

我尝试使用各种maxWidth、objectFit、width和height值的组合,如注释掉的代码所示,但似乎找不到一个能够在网页和移动视图上通用适配的解决方案。

英文:

I have been trying to fit a logo image into a parent container for a login page. This is the format for the page:

&lt;NativeBaseProvider&gt;
            &lt;View style={styles.container}&gt;
                &lt;View style={styles.logoBox}&gt; 
                    {/* &lt;Image source={images.logo} style={styles.logo} alt=&quot;Company Logo&quot;  /&gt; */}
                    {/* &lt;NativeBaseProvider&gt; */}
                                    &lt;Image  
                                        source={ images.logo }
                                        alt=&quot;Company Logo&quot; 
                                        style={styles.logo}
                                    /&gt;
                    {/* &lt;/NativeBaseProvider&gt; */}
                &lt;/View&gt;
                ... rest of login page stuff
&lt;/NativeBaseProvider&gt;

container: {
        flex: 1,
        // width: &#39;100%&#39;,
        flexDirection: &quot;column&quot;,
        alignItems: &#39;center&#39;,
        justifyContent: &#39;center&#39;,
},
logoBox: {
        // height: &#39;100%&#39;,
        height: 300,
        width: &#39;80%&#39;,
        // maxWidth: &#39;100%&#39;,
        alignItems: &#39;center&#39;,
        justifyContent: &#39;center&#39;,
        // padding: SIZES.medium,
        marginBottom: 60,
},
logo: {
        width: undefined,
        height: undefined,
        resizeMode: &#39;contain&#39;,
        flex: 1,
        // maxWidth: &#39;100%&#39;,
        // objectFit: &#39;contain&#39;,
},

The issue that I am having is that the image is a pretty large image. I am able to make it fit in the logoBox parent container when I am in web view. But when I check how it looks on the mobile app view, it is spilling off of the screen on the sides.

I have tried to use various combinations of maxWidth, objectFit, width, and height values, as seen in the commented out code, but I can't seem to find something that allows for the code to fit universally on both web and mobile views.

答案1

得分: 0

将resizeMode设置为stretch,并将高度和宽度设置为100%或flex 1。

你可以在这里阅读有关resizeMode的信息https://reactnative.dev/docs/image#resizemode

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;NativeBaseProvider&gt;
            &lt;View style={styles.container}&gt;
                &lt;View style={styles.logoBox}&gt; 
                    {/* &lt;Image source={images.logo} style={styles.logo} alt=&quot;Company Logo&quot;  /&gt; */}
                    {/* &lt;NativeBaseProvider&gt; */}
                                    &lt;Image  
                                        source={ images.logo }
                                        alt=&quot;Company Logo&quot; 
                                        style={styles.logo}
                                        resizeMode=&quot;contain&quot; // or set to stretch or cover 
                                    /&gt;
                    {/* &lt;/NativeBaseProvider&gt; */}
                &lt;/View&gt;
                ... rest of login page stuff
&lt;/NativeBaseProvider&gt;

<!-- end snippet -->

英文:

set resizeMode to stretch and give height and width to 100% or flex 1

you can read about resizeMode here https://reactnative.dev/docs/image#resizemode

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;NativeBaseProvider&gt;
            &lt;View style={styles.container}&gt;
                &lt;View style={styles.logoBox}&gt; 
                    {/* &lt;Image source={images.logo} style={styles.logo} alt=&quot;Company Logo&quot;  /&gt; */}
                    {/* &lt;NativeBaseProvider&gt; */}
                                    &lt;Image  
                                        source={ images.logo }
                                        alt=&quot;Company Logo&quot; 
                                        style={styles.logo}
                                        resizeMode=&quot;contain&quot; // or set to stretch or cover 
                                    /&gt;
                    {/* &lt;/NativeBaseProvider&gt; */}
                &lt;/View&gt;
                ... rest of login page stuff
&lt;/NativeBaseProvider&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月9日 04:09:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定