swift - 如何使 UICollectionView 透明?
问题描述
我已经尝试了几乎(字面意思?)一切来使水平集合视图透明,它覆盖在地图视图上。
这是我的收藏视图:
fileprivate let restaurantCollection : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
let restaurant = UICollectionView(frame: .zero, collectionViewLayout: layout)
restaurant.decelerationRate = .fast
restaurant.backgroundView = nil
restaurant.layer.backgroundColor = UIColor.clear.cgColor
restaurant.backgroundColor = .clear
restaurant.translatesAutoresizingMaskIntoConstraints = false
restaurant.register(MapCell.self, forCellWithReuseIdentifier: "Cell")
return restaurant
}()
如您所见,它具有清晰的背景、清晰的图层,并且背景视图为零。
并且单元格也是透明的:
func setupView() {
contentView.backgroundColor = UIColor.clear.withAlphaComponent(0)
contentView.addSubview(cellBackground)
cellBackground.translatesAutoresizingMaskIntoConstraints = false
cellBackground.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
cellBackground.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
cellBackground.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
cellBackground.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
我还向单元格“backgroundView”添加了一个视图,该单元格的所有边都固定为常数 0。这也很清楚。
lazy var cellBackground : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width, height: 220))
view.clipsToBounds = true
view.backgroundColor = .clear
view.layer.backgroundColor = UIColor.clear.cgColor
return view
}()
视图层次结构:这是仍然是白色的视图
解决方案
您需要确保设置backgroundColor
为清除
self.collectionView.backgroundColor = UIColor.clear
self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)
推荐阅读
- javascript - mark.js markRanges() 不尊重 HTML 元素
- python-3.x - 在熊猫数据框中检测星期六并修改行中的值
- python - Python Selenium iissue
- reactjs - 为什么安装webpack时会出现这种错误
- html - 缩放时如何修复我的网页布局混乱?
- apache-spark - pyspark - “col”函数的真正用途是什么
- c# - 添加另一个用户身份服务时添加身份时的“方案已存在:Identity.Application”
- bash - 如何从 awk 命令获取下一行
- istio - Istio - 每个客户端断路器配置
- python - PyQt5 GUI 无法正确加载