2017-04-11 3 views
3

Ich habe ein UICollectionView Setup wie so:Zentrieren Zellen für Paging UICollectionView

In viewDidLoad:

func setupCollectionView() { 
    collectionView.delegate = self 
    collectionView.dataSource = self 
    collectionView.register(UINib(nibName: "NewQuizzesCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "NewQuizzesCollectionViewCell") 

    let flowLayout = UICollectionViewFlowLayout() 
    flowLayout.scrollDirection = .horizontal 
    flowLayout.minimumInteritemSpacing = 20 
    flowLayout.minimumLineSpacing = 20 
    collectionView.isPagingEnabled = true 
    collectionView.setCollectionViewLayout(flowLayout, animated: false) 
} 

Und meine Delegierten:

extension NewQuizzesViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell: NewQuizzesCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewQuizzesCollectionViewCell", for: indexPath) as! NewQuizzesCollectionViewCell 
     cell.backgroundColor = colors[indexPath.row] 
     return cell 
    } 

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

} 

Wenn ich laufen die App es so aussieht :

enter image description here

Meine Frage ist, wie kann ich die Zellen zentriert machen? Ich bemerkte, dass ich den Abstand zwischen den Zellen anpassen kann, aber wie würde ich den Abstand zur linken Seite der roten Zelle hinzufügen? Irgendwelche Hinweise darauf würden wirklich geschätzt werden. Vielen Dank!

Antwort

3

diese Delegatfunktion Versuchen helfen und Sie müssen Breite von Zelle setzen Sie nach will

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionat section: Int) -> CGFloat { 
    return 0 
} 

Hoffe, dass es Sie

0

Sie diesen Code versuchen helfen, die Sie vielleicht ..

helfen
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 

    let offSet = self.collectionView.contentOffset 
    let width = self.collectionView.bounds.size.width 

    let index = round(offSet.x/width) 
    let newPoint = CGPoint(x: index * size.width, y: offSet.y) 


    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in 

    },completion: {(UIVIewTransitionCoordinatorContext) in 
     self.collectionView.reloadData() 
     self.collectionView.setContentOffset(newPoint, animated: true) 
    }) 

} 


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

für swift 3,0

minimumLineSpacingForSectionAt Funktion Verwendung für Satz Abstand zwischen den Zellen von UICollectionView-Layout ..

und müssen Sie Unterklasse von UICollectionViewDelegateFlowLayout so fügen Sie die Funktion minimumLineSpacingForSectionAt wie dieses i

class HomeBestSallingCatgCell: DatasourceCell ,UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 
    ....... 
    ...... 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 //set return 0 for no spacing you can check to change the return value 
    } 
} 

verwenden können hoffe, es wird dir nützlich sein

Verwandte Themen