2017-08-28 2 views
2

Bild in die Mitte zeigen, dass ich Bild in die Mitte zeigen wollen, In UICollectionView zuerst Artikel zeigen Bild zentriert, wenn ich auf dem zweiten Element zu verschieben, wird dieses Element nichtWie in UICollectionViewCell in Swift

zentriert
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 

    } 

Erste Bild, Punkt ist centred-

first Image, Item is centred

Zweites Bild wird Artikel nicht zentriert

second image, item is not centred

Ich bin auch horizontales Scrollen und Blättern in UICollectionView

+0

verwenden Sie Paging auf dem Collection aktiviert? Wenn ja, müssen Sie Ihre Sammlung so groß wie die Bildschirmbreite machen. – Kingalione

Antwort

0

Sie müssen benutzerdefinierte UICollectionViewFlowLayout verwenden verwenden, um zu erreichen, was Sie benötigen.

1. Erstellen einer Unterklasse von UICollectionViewFlowLayout

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 
    } 
} 

2. die Collection View Flow Layout Klasse CustomCollectionViewFlowLayout in storyboard Set.

enter image description here

Verwandte Themen