react-native - 绝对定位的 View 在 react-native 中不能作为覆盖层
问题描述
在 react-native 中添加了一个绝对定位的透明视图,以在异步 api 调用时显示进度加载器。但是覆盖层后面的输入和按钮仍然可以按下和响应。
<SafeAreaView style={styles.container}>
{this.state.isLoading &&
<View
style={{
position: 'absolute', elevation: 5, backgroundColor: 'rgba(0,0,0,0.3)',
top: 0, bottom: 0, left: 0, right: 0,
zIndex:10
}}
/>
}
<View style={{ flexGrow: 5, flexShrink: 5, flexBasis: 100, alignItems: 'center' }}>
<Image style={{ width: 200, flex: 1 }} source={require('res/images/one.png')} resizeMode='contain' />
</View>
<View style={{ flexGrow: 1, paddingLeft: 30, paddingRight: 30 }}>
<Item regular>
<Input
placeholder="username123"
autoCompleteType="username"
onChangeText={(username) => this.setState({ username })}
value={this.state.username}
/>
</Item>
<Button block onPress={this.onClick}
style={styles.button}>
<Text style={styles.buttonText}>Login</Text>
</Button>
</View>
</SafeAreaView>
PS:没有elevation:5
按钮出现在覆盖上方(使用原生按钮/控件)。没有 zIndex 的图像将出现在叠加层之上
解决方案
发生这种情况的原因是 React 组件树是如何呈现的,因为您在 Input 字段上方显示叠加层,而 Button 它们仍然在叠加层上方,您需要做的就是将叠加层从顶部移动到底部。
<SafeAreaView style={styles.container}>
<View style={{ flexGrow: 5, flexShrink: 5, flexBasis: 100, alignItems: 'center' }}>
<Image style={{ width: 200, flex: 1 }} source={require('res/images/one.png')} resizeMode='contain' />
</View>
<View style={{ flexGrow: 1, paddingLeft: 30, paddingRight: 30 }}>
<Item regular>
<Input
placeholder="username123"
autoCompleteType="username"
onChangeText={(username) => this.setState({ username })}
value={this.state.username}
/>
</Item>
<Button block onPress={this.onClick}
style={styles.button}>
<Text style={styles.buttonText}>Login</Text>
</Button>
</View>
{/* Moved below the form */}
{this.state.isLoading &&
<View
style={{
position: 'absolute', elevation: 5, backgroundColor: 'rgba(0,0,0,0.3)',
top: 0, bottom: 0, left: 0, right: 0,
zIndex:10
}}
/>
}
</SafeAreaView>
推荐阅读
- javascript - 在应用之前预览 DOM-Element 样式/布局更改
- apache - 关于 apache bench 测试模式的一些困惑
- flutter - FileSystemException:无法打开文件,路径 = 'android/app/build.gradle'
- django - 如何在 Django 中使用 force_update?
- javascript - 使用 Helmet 在 React 中注入 HTML 代码
- email - 我需要在 cypress 中创建自动化测试,它将使用不同的电子邮件注册用户并接受用户电子邮件上的注册消息
- flutter - 已超过未经验证使用的每日限制。继续使用需要注册。与谷歌驱动器一起颤抖
- ubuntu - 无法重置布局 - pgadmin (ubuntu)
- css - 下拉菜单位于 DIV 下
- python - 我可以拟合质心和峰高随机分布的多个高斯谱吗?