UICollectionView的单元格部分可见

时间:2019-03-10 13:54:50

标签: ios swift uicollectionview

我想制作一个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)
//    }
}

目标(类似于此处的收藏夹视图)

Something similar to this

我的输出

This is my output

4 个答案:

答案 0 :(得分:0)

使用这段代码

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

答案 1 :(得分:0)

添加UICollectionViewDelegateFlowLayout

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

答案 2 :(得分:0)

使用此Pod MSPeekCollectionViewDelegateImplementation

GitHub

答案 3 :(得分:0)

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

collectionView.contentInset = UIEdgeInsets.init(顶部:0,左:10,底部:0,右:10)

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