2017-04-11 127 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 
    } 
} 

我希望它对你有用