swift Collectionview单元格未与collectionview框架对齐

wbgh16ku  于 2023-06-04  发布在  Swift
关注(0)|答案(3)|浏览(281)

我正在尝试创建一个全屏集合视图单元格。集合视图被固定到父视图的边。但是collectionview单元格没有与所需的collectionview框架对齐。有人能帮忙吗?
View hierarchy
View

private func setupPageCollectionView() {
        
        let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        self.view.backgroundColor = .red
    
        pageCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        pageCollectionView.register(MyCell.self, forCellWithReuseIdentifier: MyCell.reuseIdentifier)
        pageCollectionView.delegate = self
        pageCollectionView.dataSource = self
        pageCollectionView.decelerationRate = .fast
        pageCollectionView.translatesAutoresizingMaskIntoConstraints = false
        
        pageCollectionView.backgroundColor = .blue
        self.view.addSubview(pageCollectionView)
        NSLayoutConstraint.activate([
            pageCollectionView.topAnchor.constraint(equalTo: self.view.topAnchor),
            pageCollectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            pageCollectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            pageCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
        ])
    }
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.bounds.size
    }
polkgigr

polkgigr1#

检查collectionView的contentInsets、contentInsetAdjustmentBehavior和(很可能)insetsLayoutMarginsFromSafeArea。

py49o6xq

py49o6xq2#

collectionView.bounds.size是否返回正确的大小?
也许在单元格子类中有一些顶部填充?
尝试从UICollectionViewDelegateFlowLayout实现insetForSectionAt

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    .zero
}
whitzsjs

whitzsjs3#

看起来您已正确设置了集合视图,但单元格未与集合视图框架对。要使单元格占据整个屏幕,您需要确保集合视图布局设置正确。
在代码中,您将集合视图中单元格的大小设置为与集合视图本身的大小相同。这是一个很好的开始,但是您还需要确保单元格与集合视图的边缘对齐。
为此,您可以通过添加以下代码行来修改UICollectionViewFlowLayout:

flowLayout.itemSize = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)

这将确保单元格与集合视图的大小相同,并且它们与集合视图的边缘对齐。
下面是更新的代码:

private func setupPageCollectionView() {
        
    let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .horizontal
    flowLayout.minimumInteritemSpacing = 0
    flowLayout.minimumLineSpacing = 0
    flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    
    self.view.backgroundColor = .red

    pageCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
    pageCollectionView.register(MyCell.self, forCellWithReuseIdentifier: MyCell.reuseIdentifier)
    pageCollectionView.delegate = self
    pageCollectionView.dataSource = self
    pageCollectionView.decelerationRate = .fast
    pageCollectionView.translatesAutoresizingMaskIntoConstraints = false
    
    pageCollectionView.backgroundColor = .blue
    self.view.addSubview(pageCollectionView)
    
    flowLayout.itemSize = CGSize(width: pageCollectionView.bounds.width, height: pageCollectionView.bounds.height)
    
    NSLayoutConstraint.activate([
        pageCollectionView.topAnchor.constraint(equalTo: self.view.topAnchor),
        pageCollectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
        pageCollectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
        pageCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
    ])
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return collectionView.bounds.size
}

通过设置UICollectionViewFlowLayout的itemSize属性,可以确保单元格的大小与集合视图的大小相同;通过设置集合视图的约束以匹配父视图的边缘,可以确保单元格与集合视图的边缘对齐。

相关问题