UICollectionView adds top margin

前端 未结 9 1671
走了就别回头了
走了就别回头了 2020-12-04 14:06

I want to put a UICollectionView control that shows thumbs horizontally (only a single line of thumbs). For some reason the UICollectionView push the thumbs 44 pixels down,

9条回答
  •  遥遥无期
    2020-12-04 14:24

    As some others mentioned, viewController.automaticallyAdjustsScrollViewInsets has been deprecated since iOS 11. My solution...

    Swift 4.2, Xcode 10.1, iOS 12.1:

    For some reason, collectionView.contentSize.height was appearing smaller than the resolved height of my collection view. First, I was using an auto-layout constraint relative to 1/2 of the superview's height. To fix this, I changed the constraint to be relative to the "safe area" of the view.

    This allowed me to set the cell height to vertically fill my collection view using collectionView.contentSize.height:

    private func setCellSize() {
        let height: CGFloat = (collectionView.contentSize.height) / CGFloat(numberOfRows)
        let width: CGFloat = view.frame.width - CGFloat(horizontalCellMargin * 2)
    
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: width, height: height)
    }
    

    Before

    After

提交回复
热议问题