【问题标题】:UICollectionView with cell partially visible单元格部分可见的 UICollectionView
【发布时间】:2019-03-10 13:54:50
【问题描述】:

我想做一个可以水平滚动的collectionView,只有一个单元格项目可见,一个部分可见。单元格应占据 collectionView 的整个高度。

我玩过collectionView的sectionInset。但无法达到我的目标。

我的代码:

import UIKit

class BooksController: UIViewController {

    @IBOutlet weak var booksCollectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()


        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)
        layout.itemSize = CGSize(width: booksCollectionView.frame.width, height: booksCollectionView.frame.height)
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        layout.scrollDirection = .horizontal
        booksCollectionView.collectionViewLayout = layout


        booksCollectionView.register(UINib(nibName: "BookCell", bundle: .main), forCellWithReuseIdentifier: "BookCell")
        booksCollectionView.dataSource = self
        booksCollectionView.delegate = self
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension BooksController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BookCell", for: indexPath) as? BookCell {
            return cell
        }
        return UICollectionViewCell()
    }
}

extension BooksController: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
//    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
//        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)
//    }
}

目标(此处类似于 collectionView)

我的输出

【问题讨论】:

标签: ios swift uicollectionview


【解决方案1】:

使用这段代码

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = self.view.frame.width - 100
        let height = self.view.frame.height
        return CGSize(width: width, height: height)
    }

别忘了添加这个委托

UICollectionViewDelegateFlowLayout

【讨论】:

  • 最好使用collectionView.frame 而不是self.view.frame,因为collectionView 可能小于view
  • 当 collectionView 滚动时,我需要第二个(下一个)项目应该完全可见。我试过上面的方法。但是通过这种方式,当我滚动时,第二项是不完全可见的。相反,其余部分是可见的
  • 不完全可见是什么意思?你想要快照效果吗?如果它的宽度小于集合视图,它怎么可能不完全可见?
【解决方案2】:

添加 UICollectionViewDelegateFlowLayout

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

【讨论】:

  • 当 collectionView 滚动时,我需要第二个(下一个)项目应该完全可见。我试过上面的方法。但是通过这种方式,当我滚动时,第二项是不完全可见的。相反,其余部分是可见的
【解决方案3】:

使用这个 Pod MSPeekCollectionViewDelegateImplementation

GitHub

【讨论】:

    【解决方案4】:

    如果需要显示第二个集合视图单元格的一半,可以使用

    collectionView.contentInset = UIEdgeInsets.init(上:0,左:10,下:0,右:10)

    对于集合视图的高度和搭配,您可以尝试 CollectionViewLayout 委托

    【讨论】:

    • 在情节提要中,删除 UICollectionView 中的默认插图。
    猜你喜欢
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2017-01-01
    相关资源
    最近更新 更多