【问题标题】:collectionViewLayout with items aligned to the left in Swift在 Swift 中,项目左对齐的 collectionViewLayout
【发布时间】:2021-07-17 18:05:09
【问题描述】:

我有一个 collectionView,其中包含来自服务请求的图像,这些图像以 3 个元素的形式显示。 我需要的是,当请求中只有一个或两个图像时,图像显示为左对齐,但屏幕显示图像居中。

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if imageFuelArray.count == 1{
            let padding: CGFloat =  10
            let collectionViewWidth = collectionView.frame.size.width - padding
            let collectionViewHeight = collectionView.frame.size.height - padding
            
            return CGSize(width: collectionViewWidth, height: collectionViewHeight)
        } else{
            let padding: CGFloat =  10
            let collectionViewWidth = collectionView.frame.size.width - padding
            let collectionViewHeight = collectionView.frame.size.height - padding
            
            return CGSize(width: collectionViewWidth/2, height: collectionViewHeight/2)
        }
    }

【问题讨论】:

    标签: ios swift alignment uicollectionviewlayout collectionview


    【解决方案1】:

    我有这个功能

    func getSizeVews(){
             if imageFuelArray.count > 6{
                self.collectionView.heightAnchor.constraint(equalToConstant: self.collectionView.contentSize.height+45).isActive = true
                                  self.servicesTableView.heightAnchor.constraint(equalToConstant: self.servicesTableView.contentSize.height+70).isActive = true
                                  
                                  view.layoutSubviews()
             } else {
                self.collectionView.heightAnchor.constraint(equalToConstant: self.collectionView.contentSize.height).isActive = true
                       self.servicesTableView.heightAnchor.constraint(equalToConstant: self.servicesTableView.contentSize.height+70).isActive = true
                       
                       view.layoutSubviews()
            }
           
        }
    

    还有我的 collectionView 数据源和委托

    extension ServicesStationViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            imageFuelArray.count
        }
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: flCollectionVieCell, for: indexPath) as! FuelCollectionViewCell
            let fuel = imageFuelArray[indexPath.row]
            
            if let cepsaFuel = CepsaFeaturesFuel(key: Int(fuel) ?? 0) {
                let imgName = CepsaFeaturesFuel.featureFuelImageName(with: cepsaFuel.featuresFuelType)
                
                cell.fuelImageView?.image = UIImage(named: imgName!)
            }
            return cell
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            if imageFuelArray.count == 1{
                let padding: CGFloat =  10
                let collectionViewWidth = collectionView.frame.size.width - padding
                let collectionViewHeight = collectionView.frame.size.height - padding
                
                return CGSize(width: collectionViewWidth, height: collectionViewHeight)
            } else{
                let padding: CGFloat =  10
                let collectionViewWidth = collectionView.frame.size.width - padding
                let collectionViewHeight = collectionView.frame.size.height - padding
                
                return CGSize(width: collectionViewWidth/2, height: collectionViewHeight/2)
            }
        }
        
    }
    

    【讨论】:

      【解决方案2】:

      尝试计算单元格的宽度,使其始终为可用宽度的 1/3:

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
          let numberOfItemsPerRow: CGFloat = 3
          let spacingBetweenCells: CGFloat = 10
              
          let totalSpacing = (2 * spacingBetweenCells) + ((numberOfItemsPerRow - 1) * spacingBetweenCells)
              
          let width = (collectionView.bounds.width - totalSpacing) / numberOfItemsPerRow
          let height = collectionView.bounds.height - totalSpacing
              
          return CGSize(width: width, height: height)
      }
      

      【讨论】:

      • 它不起作用,当列表只有一个时仍然显示居中的项目:(
      • @simojo 你能发布你的完整集合视图委托和数据源代码吗?
      • 现在你有了一个新的答案(没有你的修改),感谢你的帮助! @luca_999
      • 尝试从扩展中删除UICollectionViewDelegate,使其保持这种extension ServicesStationViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
      猜你喜欢
      • 2017-11-14
      • 2021-02-21
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      相关资源
      最近更新 更多