You need to enable JavaScript to run this app. 如何在视图中设置Expo BarCodeScanner的宽高并添加边框
优惠活动
大模型
产品
解决方案
定价
更多

如何在视图中设置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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 11:54:03