【react native】css踩坑记录

1、IOS上面opacity重叠失效

在 iOS 上,当两个具有相同背景色的元素重叠时,不透明度(opacity)较低的元素会显示在较高的元素上方。

所以考虑使用rgba的形式。

javascript 复制代码
// 对于下面这种写法,如果存在container和activeIndicator重叠,则始终会展示container的颜色
const styles = StyleSheet.create({
  container: {
    height: 6,
    // 在 iOS 上,当两个具有相同背景色的元素重叠时,
    // 不透明度(opacity)较低的元素会显示在较高的元素上方。
    // 所以需要改用 rgba 的形式
    backgroundColor: '#ffffff',
    opacity: 0.51,
    flex: 1,
    borderRadius: 8,
    marginHorizontal: 3,
    overflow: 'hidden',
  },
  activeIndicator: {
    flex: 1,
    backgroundColor: '#ffffff',
  },
});

// 修改后
const styles = StyleSheet.create({
  container: {
    height: 6,
    backgroundColor: 'rgba(255, 255, 255, 0.51)', // here
    flex: 1,
    borderRadius: 8,
    marginHorizontal: 3,
    overflow: 'hidden',
  },
  activeIndicator: {
    flex: 1,
    backgroundColor: 'rgba(255, 255, 255, 1)', // here
  },
});

2、Image使用,图片不能撑满整个容器

image#resizemode

例如,对于下面这段代码可能存在这样的情况,图片不能撑满整个容器,上下会存在空隙。

javascript 复制代码
<View style={styles.bgContainer}>
  <Image
    source={{ uri: image }}
    resizeMode='contain' // 更新为 'cover'
    style={styles.bg}
  />
</View>

bgContainer: {
  position: 'absolute',
  top: 0,
  bottom: 0,
  left: 0,
  right: 0,
  // backgroundColor: 'yellow',
},
bg: {
  width: WINDOW_WIDTH,
  height: WINDOW_HEIGHT,
  // backgroundColor: 'blue',
},

将 resizeMode 属性设置为 'contain',这会导致图片按照原始比例进行缩放,以适应容器的尺寸。如果图片的宽高比与容器的宽高比不匹配,那么图片可能无法填满整个容器。

如果希望图片填满整个容器,可以尝试将 resizeMode 属性设置为 'cover',这样图片会被拉伸或压缩以填满容器。

相关推荐
htzyl2066 小时前
前端样式-弹窗淡入淡出,为什么你的 `transition` 一直不生效
前端·css·html
Brookty6 小时前
CSS基础知识(二):盒模型、内外边距、外边距折叠与父子元素的布局关系
前端·css
用户2986985301413 小时前
React 前端实现 Word 与 OpenXML 双向转换的完整实践
javascript·react.js
Mh1 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
Brookty1 天前
CSS基础知识(一):选择器、优先级、层叠与继承、边框属性及盒模型
前端·css
htzyl2061 天前
CSS中,`:has()` 能帮你删掉的 4 类 JS
前端·css
月夏2 天前
我把全栈项目塞进已有 nginx 的服务器,卡我最久的是三个斜杠
nginx·react.js·docker
jianqiang.xue2 天前
Espressif-IDE进入维护模式:乐鑫把ESP32开发的宝押给VS Code
前端·css·ide
feixing_fx2 天前
CSS 变量(Custom Properties):构建可扩展的主题切换系统
前端·css·前端框架·html·css3
晓得迷路了4 天前
栗子前端技术周刊第 154 期 - Parcel 3 Alpha、JS 调查报告、Vite 漏洞...
前端·javascript·css