如何在视图中设置Expo BarCodeScanner的宽高并添加边框
问题原因
你给BarCodeScanner设置了StyleSheet.absoluteFillObject属性,该属性会让扫码器组件完全覆盖父容器的所有空间(包括边框区域),所以父容器设置的边框被压在扫码器图层下方无法显示。
解决方案
方案1:调整扫码器的绝对填充偏移
修改BarCodeScanner的样式,给四个边设置和父容器边框宽度相等的偏移量,避免覆盖边框:
<View style={styles.scannerContainer}> <BarCodeScanner onBarCodeScanned={(hasScan) ? undefined : handleScan} barCodeTypes={[BarCodeScanner.Constants.BarCodeType.qr]} style={{ ...StyleSheet.absoluteFillObject, // 偏移值和父容器的borderWidth保持一致即可 top: 5, left: 5, right: 5, bottom: 5 }} /> </View>
方案2:给父容器添加内边距
在父容器scannerContainer的样式中添加和边框宽度相等的内边距,让扫码器仅填充边框以内的内容区域:
scannerContainer: { flex: 1, alignItems: 'center', justifyContent: 'center', width: vw(100), maxHeight: vh(50), backgroundColor: 'red', borderColor: colors.primary, borderWidth: 5, // 新增内边距,值和borderWidth保持一致 padding: 5 }
内容的提问来源于stack exchange,提问作者laneherby
相关产品推荐
相关产品推荐

