react-native - React modal 元素不覆盖屏幕的左侧
问题描述
我有一个Modal
我想要在整个屏幕上的元素,为此我创建了函数widthPercentageToDP
并heightPercentageToDP
从设备获取宽度和高度并根据设置的百分比填充它,高度被完全覆盖但宽度有左侧的小间隙未被覆盖。
import { StyleSheet, Dimensions, PixelRatio } from 'react-native';
<Modal isVisible={true}>
<View style={[s.contract]} >
<View >
<Text>
I want the modal on the whole screen
</Text>
</View>
<View >
{previousButton}{nextButton}
</View>
</View>
</Modal>
const styles = StyleSheet.create({
contract: {
backgroundColor: 'white',
width: widthPercentageToDP('100%'),
height: heightPercentageToDP('100%')
}
});
const widthPercentageToDP = widthPercent => {
const screenWidth = Dimensions.get('window').width;
const elemWidth = parseFloat(widthPercent);
return PixelRatio.roundToNearestPixel(screenWidth * elemWidth / 100);
};
const heightPercentageToDP = heightPercent => {
const screenHeight = Dimensions.get('window').height;
const elemHeight = parseFloat(heightPercent);
return PixelRatio.roundToNearestPixel(screenHeight * elemHeight / 100);
};
解决方案
为什么不用css?为您的Modal 组件提供模态css-class 并将后者定义为:
.modal {
background:white;
box-shadow:0 2px 8px rgba(0,0,0, 0.26)
width:90%;
position: fixed;
top:20vh;
left:5%;
}
media (min-width:768px) {
.modal {
width: 50rem;
left:calc((100%-50rem) / 2)
}
}
这是一个尚未完成但工作 的代码框
推荐阅读
- python - 如何让 datetime 不断检查时间并在 discord.py 中发送消息
- c# - 如何安装 Cefsharp
- android - 是否有在 android API 17+ 中定义目录选择器的本地方法?
- python - 为什么python抛出错误:AttributeError:'numpy.ndarray'对象没有属性'append'?
- javascript - 我能否确定浏览器中的可访问性功能是否处于活动状态?
- android - 如何在 Jetpack Compose 中制作手动可滚动选取框(通过触摸或鼠标/触摸板滚动)?
- aframe - 移动鼠标时桌面版本出错(cursor.js)
- angular - 将 aws-exports.js 移动到另一个 Angular 项目?
- modelica - 从 Dymola 导出具有大参数数组的 FMU
- c - 我无法弄清楚我是如何错误地创建了这个文件的