我如何使用UICollectionView
在滚动时构建带有项目间距的分页图像库,但项目应该是全屏的?
如果我订
flowLayout.minimumInteritemSpacing = 10.0
flowLayout.minimumLineSpacing = 10.0
这个间距将永远存在,分页将被打破
添加项目间或行间距,就像在帖子中一样。然后使集合视图以与间距相同的量超出屏幕边界。所以,如果你在项目之间加10 pt,那么集合视图应该在每一边伸出5磅(我想;我可能会偏离2倍)。
这是我在iOS 9,Swift中必须做的事情:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
//UIScreen.mainScreen().bounds.width
return CGSizeMake(self.collectionView.bounds.size.width - 6, self.collectionView.bounds.size.height)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 6
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 3, 0, 3)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 6
}
因此,正如上面的代码所示,每个UICollectionViewCell在滚动时彼此之间有6px。
对于viewDidLoad中的行间距10,设置collectionView.frame = CGRect(x:-5,y:0,width:UIScreen.main.bounds.width + 10,height:UIScreen.main.bounds.height),然后在UICollectionViewDelegateFlowLayout中:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return view.bounds.size
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
}
它对我有用。希望它对某人有帮助