2017-04-11 156 views
3

我有一個UICollectionView設置像這樣:圍繞細胞尋呼UICollectionView

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

而且我代表:

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

} 

當我運行它看起來像這樣的應用程序:

enter image description here

我的問題是,我怎樣才能使細胞居中?我注意到我可以調整單元格之間的間距,但是如何將間距添加到紅色單元格的左側?任何關於此的指針將非常感激。謝謝!

回答

3

試試這個委託功能會幫助你,你需要根據你設置單元格的寬度要

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

希望它會幫助你

0

你嘗試這種代碼,可以幫助你..

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

SWIFT 3.0

minimumLineSpacingForSectionAt功能使用了UICollectionView佈局單元格之間的間距設置..

,你需要添加UICollectionViewDelegateFlowLayout的子類,所以你可以使用函數minimumLineSpacingForSectionAt這樣

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

我希望它對你有用