[英]UICollectionView horizontal scrolling
我有一個UICollectionView
與水平滾動。 這是我的collectionView
:
fileprivate(set) lazy var collectionView: UICollectionView = {
let width = UIScreen.main.bounds.width.multiplied(by: 0.9)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: width, height: 50)
layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 20
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: 50), collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = .red
collectionView.isPagingEnabled = true
return collectionView
}()
它看起來像這樣:
如您所見,由於我希望分頁效果,所以代碼中具有collectionView.isPagingEnabled = true
。 所以我想要實現的是使項目看起來像其他每頁中的上圖(左右20個間距)一樣,但是到目前為止,我得到了:
任何想法/技巧如何達到所需的行為?
這是我在測試項目中使用的UICollectionViewDelegateFlowLayout
來實現所需的功能。
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width.multiplied(by: 0.9), height: 50.0)
}
// item spacing = vertical spacing in horizontal flow
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return (UIScreen.main.bounds.width.multiplied(by: 0.1))
}
// line spacing = horizontal spacing in horizontal flow
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return (UIScreen.main.bounds.width.multiplied(by: 0.1))
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: (UIScreen.main.bounds.width.multiplied(by: 0.1) / 2.0), bottom: 0, right: (UIScreen.main.bounds.width.multiplied(by: 0.1) / 2.0))
}
與您的代碼將是這樣的:
fileprivate(set) lazy var collectionView: UICollectionView = {
let width = UIScreen.main.bounds.width.multiplied(by: 0.9)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: width, height: 50)
layout.sectionInset = UIEdgeInsets(top: 20, left: UIScreen.main.bounds.width.multiplied(by: 0.1) / 2.0, bottom: 10, right: UIScreen.main.bounds.width.multiplied(by: 0.1) / 2.0)
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = UIScreen.main.bounds.width.multiplied(by: 0.1)
layout.minimumInteritemSpacing = UIScreen.main.bounds.width.multiplied(by: 0.1) // or any value you want
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: 50), collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = .red
collectionView.isPagingEnabled = true
return collectionView
}()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.