如何在Swift中的UICollectionViewCell中将图像显示到中心

时间:2017-08-28 07:17:18

标签: swift swift3 uicollectionview

我想将图像显示到中心,在UICollectionView中,当我移动第二个项目时,第一个项目显示图像居中,此项目不居中

  let itemsPerRow:CGFloat = 1
  var sectionInsets =  UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)
  var productImage = [String]()


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

    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductInfoDetailCell", for: indexPath)  as! ProductInfoDetailCell
        cell.productLargeImageView.image = UIImage(named: productImage[indexPath.row])
        //cell.productLabel.text = productTitle[indexPath.row]
        return cell
    }




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

        let paddingSpace = (sectionInsets.left ) * (itemsPerRow + 1)
        let availableWidth = collectionView.frame.size.width - paddingSpace
        let widthPerItem = availableWidth / itemsPerRow
        return CGSize(width: widthPerItem , height: 400)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return sectionInsets
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

        return sectionInsets.left

    }

第一张图片,项目居中 -

first Image, Item is centred

第二张图片,商品未居中

second image, item is not centred

我也在UICollectionView中使用水平滚动和分页

1 个答案:

答案 0 :(得分:0)

您需要使用自定义var handle = Policy.Handle<Exception>();//.OrResult<HttpResponseMessage>(r => r.IsSuccessStatusCode == false); var timeout = Policy.TimeoutAsync(() => TimeSpan.FromMinutes(5) /*loginConnectorOptions.Timeout*/); var retry = handle.RetryAsync(retryCount: 3); var cb = handle.CircuitBreakerAsync(exceptionsAllowedBeforeBreaking: 3, durationOfBreak: TimeSpan.FromMinutes(3)); var bulkhead = Policy.BulkheadAsync(maxParallelization: 4, maxQueuingActions: 20); _lcPolicy = Policy.WrapAsync(bulkhead, retry, cb, timeout); _lcPolicy.WithPolicyKey("LoginConnector"); 来实现您的需求。

1。创建UICollectionViewFlowLayout

的子类
UICollectionViewFlowLayout

2。class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout { override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { if let collectionViewBounds = self.collectionView?.bounds { let halfWidthOfVC = collectionViewBounds.size.width * 0.5 let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidthOfVC if let attributesForVisibleCells = self.layoutAttributesForElements(in: collectionViewBounds) { var candidateAttribute : UICollectionViewLayoutAttributes? for attributes in attributesForVisibleCells { let candAttr : UICollectionViewLayoutAttributes? = candidateAttribute if candAttr != nil { let a = attributes.center.x - proposedContentOffsetCenterX let b = candAttr!.center.x - proposedContentOffsetCenterX if fabs(a) < fabs(b) { candidateAttribute = attributes } } else { candidateAttribute = attributes continue } } if candidateAttribute != nil { return CGPoint(x: candidateAttribute!.center.x - halfWidthOfVC, y: proposedContentOffset.y); } } } return CGPoint.zero } } 中将Collection View Flow Layout课程设为CustomCollectionViewFlowLayout

enter image description here