简体   繁体   English

Swift:按屏幕分页不适用于两列布局集合视图

[英]Swift: paging by screen doesn't work for two column layout collection view

I have a collectionview controller with custom 2-columns layout:我有一个带有自定义 2 列布局的 collectionview 控制器:

class CVLayout: UICollectionViewFlowLayout {

override init() {
    super.init()
    setupLayout()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupLayout()
}

override var itemSize: CGSize {
    set {

    }
    get {
        let numberOfColumns: CGFloat = 2

        let itemWidth = (self.collectionView!.frame.width - (numberOfColumns - 1)) / numberOfColumns
        return CGSize(width: itemWidth, height: itemWidth)
    }
}

func setupLayout() {
    minimumInteritemSpacing = 1
    minimumLineSpacing = 1
    scrollDirection = .horizontal
}

} }

In my controller I set在我的控制器中我设置

class ViewController: UICollectionViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    collectionView?.isScrollEnabled = true
    collectionView?.isPagingEnabled = true
}

I have 7 items, 6 items are displayed on the screen and I expect to see one item on the next page, but instead I see 4 (3 from the previous page) I tried to play with number of sections and items in section, but it doesn't help.我有 7 个项目,屏幕上显示 6 个项目,我希望在下一页看到一个项目,但我看到 4 个(上一页中的 3 个)它没有帮助。 What I've found from other topics is that default paging should be by screen, not by cell (in my case a column).我从其他主题中发现默认分页应该是按屏幕,而不是按单元格(在我的情况下是一列)。 What am I doing wrong?我究竟做错了什么?

If I understand your question, I do not think you are doing anything wrong per se.如果我理解你的问题,我不认为你做错了什么。 The content size of the collection view is determined (in part) by the item size.集合视图的内容大小(部分)由项目大小决定。 Right now that is approximately the width of 3 items (or a page and a half of content).现在这大约是 3 个项目的宽度(或一页半的内容)。 I think you need to override the collection view's content size property to reflect full pages of content (in width) to achieve the desired effect.我认为您需要覆盖集合视图的内容大小属性以反映完整的内容页面(宽度)以达到所需的效果。

override var collectionViewContentSize: CGSize {

    if  let collectionView = collectionView {
        let numberOfItems = collectionView.numberOfItems(inSection: 0)
        let pages = numberOfItems/2
        let size =  CGSize(width: collectionView.frame.width * CGFloat(pages), height: collectionView.frame.height)
        return size
    }

    return CGSize(width: 0, height: 0)
}

Please see this answer How to expand UICollectionView contentSize when paging enable?请参阅此答案如何在启用分页时扩展 UICollectionView contentSize? . . It is a bit old but I think it is trying to solve the same issue.它有点旧,但我认为它正在尝试解决同样的问题。

First of all UICollectionViewFlowLayout doesn't support paging首先 UICollectionViewFlowLayout 不支持分页

There are two solution that i think我认为有两种解决方案

first第一的

use UIScrollView add 7 items simply with paging option使用 UIScrollView 添加 7 个项目,只需使用分页选项

second第二

use UICollectionView modifying your UICollectionViewFlowLayout使用 UICollectionView 修改您的 UICollectionViewFlowLayout

subclass UICollectionViewFlowLayout to support paging子类 UICollectionViewFlowLayout 以支持分页

ex)前任)

In Swift3在 Swift3 中

class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        var offsetAdjustment = CGFloat.greatestFiniteMagnitude
        let horizontalOffset = proposedContentOffset.x
        let targetRect = CGRect(x: proposedContentOffset.x, y: 0, width: self.collectionView!.bounds.size.width, height: self.collectionView!.bounds.size.height)
        for layoutAttributes in super.layoutAttributesForElements(in: targetRect)! {
            let itemOffset = layoutAttributes.frame.origin.x
            if abs(itemOffset - horizontalOffset) < abs(offsetAdjustment){
                offsetAdjustment = itemOffset - horizontalOffset
            }
        }
        return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y)
    }
}

you can find more information searching UICollectionViewFlowLayout with Paging您可以找到更多信息, UICollectionViewFlowLayout with Paging搜索UICollectionViewFlowLayout with Paging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM