菜鸟教程小白 发表于 2022-12-11 19:30:17

ios - 为什么 zIndex 在 React Native 中不起作用?


                                            <p><p>我一直在尝试在 <code>ScrollView</code> 上使用属性 <code>zIndex</code>,但它似乎不起作用。当我应用 <code>zIndex</code> 时,它看起来像这样:</p>

<p> <a href="/image/ZU8AA.png" rel="noreferrer noopener nofollow"><img src="/image/ZU8AA.png" alt="enter image description here"/></a> </p>

<p>如您所见,<code>ScrollView</code> 被输入阻止。当建议出现时,我希望 <code>ScrollView</code> 使用 <code>zIndex</code> 完全覆盖其他输入的效果。这是我现在拥有的:</p>

<pre><code>&lt;View&gt;
    &lt;Input
      value={this.state.inputValue}
      style={styles._input}
      inputStyle={styles._text}
      onChangeText={this.handleChangeText}
      {...this.props}
    /&gt;
    &lt;View style={styles._spacing}&gt;&lt;/View&gt;
    {predictions.length ?
      &lt;ScrollView
            style={styles._scroll}
      &gt;
            {predictions.map(this.renderPrediction)}
      &lt;/ScrollView&gt;
    : null}
&lt;/View&gt;
</code></pre>

<p>请注意,<code>Input</code> 是呈现特定 <code>TextInput</code> 的单独组件。此外,<code>this.renderPrediction</code> 返回:</p>

<pre><code>&lt;Text
    style={styles._text}
    key={index}
    onPress={this.handlePredictionPress.bind(null, prediction.description)}
&gt;
    {prediction.description}
&lt;/Text&gt;
</code></pre>

<p>最后是我的风格:</p>

<pre><code>const styles = EStyleSheet.create({
    input: StyleSheet.flatten(),
    text: {
      fontSize: 15,
    },

    spacing: {
      height: 10
    },

    scroll: {
      position: &#34;absolute&#34;,
      zIndex: 10,
      width: &#34;80%&#34;,
      top: 50,
      backgroundColor: &#34;white&#34;
    }
});
</code></pre>

<p>为什么我的 <code>zIndex</code> 尝试不工作,我怎样才能让它按需要工作?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>根据您的图像,看起来 <code>zIndex</code> 正在按预期工作,<code>ScrollView</code> 在输入上方。但是白色背景没有捕捉到。</p>

<p>使用 <code>contentContainerStyle</code> 作为项目背后的背景颜色。</p>

<pre><code>&lt;ScrollView style={styles._scroll} contentContainerStyle={styles._contentContainer} &gt;

...
contentContainer: {backgroundColor: &#34;white&#34;}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 为什么 zIndex 在 React Native 中不起作用?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43460916/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43460916/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 为什么 zIndex 在 React Native 中不起作用?