简体   繁体   English

如何使 UICollectionView 透明?

[英]How do I make a UICollectionView transparent?

I have tried virtually (literally?) everything to make a horizontal collection view transparent which is overlaid on a map view.我几乎(字面意思?)尝试了一切以使水平集合视图透明,该视图覆盖在 map 视图上。

This is my collection view:这是我的收藏视图:

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

As you can see it has a clear background, clear layer and the background view is nil.如您所见,它具有清晰的背景、清晰的图层,并且背景视图为零。

And the cell is transparent as well:并且单元格也是透明的:

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
    }

I also added a view to the cell "backgroundView" that's pinned with constant 0 to all sides.我还向单元格“backgroundView”添加了一个视图,该单元格的所有边都固定为常数 0。 And this too is clear.这也很清楚。

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

And after all that effort, the collection view still has a white background经过所有这些努力,集合视图仍然具有白色背景在此处输入图像描述

View hierarchy: Here is the view that is still white视图层次结构:这是仍然是白色的视图

在此处输入图像描述 在此处输入图像描述

You need to make sure that you are setting backgroundColor to clear您需要确保将backgroundColor设置为清除

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

For Modern List Layout CollectionViews:对于现代列表布局 CollectionViews:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swift 3 中使这个 UITableView 清晰(透明) - How do I make this UITableView clear (transparent) in Swift 3 如何使我的导航栏在swift中透明? - How do I make my navigation bar transparent in swift? 如何在Swift中使iPhone状态栏透明? - How do I make the iPhone status bar transparent in Swift? Swift-如何使用uicollectionview做到这一点? - Swift - How could I use uicollectionview to make this? 如何使uicollectionview不使单元出队? - How can I make the uicollectionview not dequeue the cells? 当所有单元格的大小都不相同时,如何包装UICollectionView的单元格? - How do I make a UICollectionView's cells wrap when all cells are not the same size? 我如何使我的UICollectionView返回3列,它们之间的间距为1像素,就像instagram一样? 迅速 - How do I make my UICollectionView return 3 columns with 1 pixel spacing between them just like instagram? Swift 如何在UICollectionView的页脚中添加UIActivityIndi​​catorView? - How do I add a UIActivityIndicatorView to footer of UICollectionView? 如何使用Swift为UICollectionView插入动画? - How do I animate UICollectionView insert with Swift? 如何在UICollectionViewCell内添加UICollectionView? - How do I add a UICollectionView inside a UICollectionViewCell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM