Swift iOS如何删除顶部填充UICollectionView

时间:2018-10-23 09:52:08

标签: ios swift uicollectionview uicollectionviewcell

override func viewDidLoad(){
    super.viewDidLoad()
    scrollView.collectionViewLayout = layout
    scrollView.delegate = self
    scrollView.dataSource = self
    scrollView.contentInset = .zero
    scrollView.scrollIndicatorInsets = .zero
    scrollView.isDirectionalLockEnabled = true
    scrollView.register(UINib(nibName: "RadioView", bundle: nil), forCellWithReuseIdentifier: "RadioViewController")
}
lazy var layout: UICollectionViewFlowLayout = {
    let layout = UICollectionViewFlowLayout()
    layout.minimumLineSpacing = 0
    layout.headerReferenceSize = .zero
    layout.sectionInset = .zero
    layout.scrollDirection = UICollectionViewScrollDirection.horizontal
    return layout
}()

背景色红色为UICollectionView

backgorund黑色为UICollectionViewCell

enter image description here

我想将Cell移到左上方

enter image description here enter image description here

3 个答案:

答案 0 :(得分:1)

如果不是布局约束问题,则:

collectionView.contentInset = UIEdgeInsets.zero

也可能已实现:

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

在这种情况下,您需要将方法的返回值top属性设置为0

答案 1 :(得分:0)

尝试在UIViewController中设置viewDidLoad的这两个属性

extendedLayoutIncludesOpaqueBars = true
automaticallyAdjustsScrollViewInsets = false

请检查情节提要中的标题大小,尝试将其设置为零。

答案 2 :(得分:0)

我认为这是将Y component的值设置为零的问题

Problem is here