UICollectionView Custom Cell to fill width In Swift

后端 未结 11 2011
不思量自难忘°
不思量自难忘° 2021-01-30 21:12

I\'ve been trying to figure-out how can i make the cell fill the width, as you can see in the picture the width between the cells is too big. i am using custom cell with only on

相关标签:
11条回答
  • 2021-01-30 21:44

    The best solution is to change the itemSize of your UICollectionViewFlowLayout in viewDidLayoutSubviews. That is where you can get the most accurate size of the UICollectionView. You don't need to call invalidate on your layout, that will be done for you already.

    0 讨论(0)
  • 2021-01-30 21:45

    Swift 3

    If you are using swift 3, use this method:

        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        }
    

    Notice the changes:

    1. add _ before collectionView in the method name
    2. NSIndexPath changes to IndexPath
    0 讨论(0)
  • 2021-01-30 21:46

    Programatically set the itemSize to [UIScreen mainScreen].bounds.size.width/3

    0 讨论(0)
  • 2021-01-30 21:46

    For my case, autolayout was only allowing the cell to grow to its content size even though I overrode the cell size in the storyboard, conformed to UICollectionViewDelegateFlowLayout, and implemented sizeForItemAt. So I made a dummy UIView the width of the cell's bounds.

    UICollectionViewController:

    extension CollectionViewController: UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let width = collectionView.bounds.width - 40
            return CGSize(width: width, height: collectionView.bounds.height / 3)
        }
    }
    

    Cell (I'm calling this method on dependency injection):

    private func updateViews() {
        let padding:CGFloat = 8
        let innerView = UIView()
        innerView.translatesAutoresizingMaskIntoConstraints = false
        innerView.layer.cornerRadius = 8
        innerView.layer.borderColor = UIColor.black.cgColor
        innerView.layer.borderWidth = 1
        contentView.addSubview(innerView)
        NSLayoutConstraint.activate([
            innerView.widthAnchor.constraint(equalToConstant: bounds.width - padding*2),
            innerView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: padding),
            innerView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -padding),
            innerView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: padding),
            innerView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -padding)
        ])
    }
    
    0 讨论(0)
  • 2021-01-30 21:50

    Also in Swift 3,

    Make sure your view controller complies with the following:

    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout

    0 讨论(0)
  • 2021-01-30 21:51

    For my case, I wanted to show two columns and 3 rows in the UICollectionView

    I added UICollectionViewDelegateFlowLayout delegate to my class

    then I override sizeForItemAt indexPath

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellHeight = (collectionView.bounds.size.height - 30) / 3 // 3 count of rows to show
        let cellWidth = (collectionView.bounds.size.width - 20) / 2 // 2 count of colomn to show
        return CGSize(width: CGFloat(cellWidth), height: CGFloat(cellHeight))
    }
    

    The 30 is the Line spacing, the 20 is the insent between cells

    0 讨论(0)
提交回复
热议问题