Swift scrollToItem并居中于单元格的中间

时间:2017-09-22 16:07:46

标签: ios swift swift3 uiscrollview uicollectionview

我一直无法理解如何以编程方式在CollectionView中使用scrollToItem并将单元格置于屏幕中间。似乎当我使用scrollToItem时,它会水平滚动到项目,但是在我要滚动到的单元格的开头停止而不是在它的中心。非常感谢任何帮助或见解。

override func viewDidLoad() {
    super.viewDidLoad()
    self.cv.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    self.view.layoutIfNeeded()
}


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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
         return UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
         return 0
     }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

0 个答案:

没有答案