簡體   English   中英

如何使 UICollectionView 透明?

[英]How do I make a UICollectionView transparent?

我幾乎(字面意思?)嘗試了一切以使水平集合視圖透明,該視圖覆蓋在 map 視圖上。

這是我的收藏視圖:

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)

對於現代列表布局 CollectionViews:

UICollectionLayoutListConfiguration.backgroundColor = .clear
UICollectionViewListCell.backgroundConfiguration = .clear()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM