【问题标题】:How do I make a UICollectionView transparent?如何使 UICollectionView 透明?
【发布时间】:2020-05-11 19:02:50
【问题描述】:

我已经尝试了几乎(字面意思?)所有方法来使覆盖在地图视图上的水平集合视图透明。

这是我的收藏视图:

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
}()

在所有这些努力之后,集合视图仍然具有白色背景

视图层次结构:这是仍然是白色的视图

【问题讨论】:

  • 除了 contentView 之外的图像后面的任何视图
  • 确保将视图的 isOpaque 属性设置为 false
  • 检查 View Hierarchy 以查看它是白色的视图 ....
  • 我检查了视图层次结构,问题出在 UICollectionView 上。但是,我已经设置了 isOpaque = false

标签: swift collectionview


【解决方案1】:

您需要确保将backgroundColor 设置为清除

    self.collectionView.backgroundColor = UIColor.clear
    self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 2011-11-17
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多