2017-09-03 80 views
0

我正在使用UICollectionView,並且到目前爲止我已經做得很好。我被困在UIViewLayout中,我想讓單元居中。 我想讓我的UICollectionView單元格中心。請看下面的圖片。圖1是我通過下面的代碼實現的圖像2是我想要的。請幫助傢伙。 TIA 圖片 - 1張 Image 1在Swift 4中心UICollectionViewCells

圖片 - 2 Image 2

看看我的代碼段。

import UIKit 

class ViewController: UIViewController { 

var collectionView: UICollectionView! 
var sizingCell = StateCollectionViewCell() 
var states = ["Alabama", "Alaska", "Arizona", "California", 
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", 
"Idaho", "Indiana", "Lowas", "Kansas", "New Jersey", "Mexico", 
"Missori", "Montana", "Newada", "New York", "Maine", "Maryland", 
"Kentchuky"] 

override func viewDidLoad() { 
super.viewDidLoad() 
// Do any additional setup after loading the view, typically from a 
nib. 
setupCollectionView() 

    let layout = collectionView?.collectionViewLayout as! 
    UICollectionViewFlowLayout 
    layout.sectionInset = UIEdgeInsets(top: 100, left: 15, bottom: 10, 
    right: 20) 
    layout.estimatedItemSize = CGSize(width: 100, height: 100) 
    layout.itemSize = UICollectionViewFlowLayoutAutomaticSize 
    } 

    // Mark: - Initializing a collectionView and addint it to the VC's 
    current view 
    func setupCollectionView() { 
    let layout = UICollectionViewFlowLayout() 
    collectionView = UICollectionView(frame: view.frame, 
    collectionViewLayout: layout) 
    collectionView.register(StateCollectionViewCell.self, 
    forCellWithReuseIdentifier: "stateCell") 
    collectionView.backgroundColor = UIColor.white 
    collectionView.delegate = self 
    collectionView.dataSource = self 

    view.addSubview(collectionView) 
    } 

    } 

    extension ViewController: UICollectionViewDelegate, 
    UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

    //Mark: - Specifying the number of sections in the collectionView 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
    } 

    //Mark: - Specifying the number of cells in given section 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
return states.count 

    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "stateCell", for: indexPath)as! stateCollectionViewCell 
cell.awakeFromNib() 
return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
let stateCell = cell as! StateCollectionViewCell 
stateCell.stateCell.text = states[indexPath.row] 

    } 

} 
+0

嘗試此github [鏈接](https://github.com/keighl/KTCenterFlowLayout/blob/master/KTCenterFlowLayout.m) –

+0

是的,我已經看到了這一點,但想要簡單,就像我當前的代碼中的更改。對於鏈接,您提供了我需要添加豆莢並進行許多更改。所以把它放在一邊。 @AdityaSrivastava – iDeveloper

+0

@iDeveloper僅僅因爲一些UI界面看起來很簡單,並不意味着實現它的過程很簡單。簡單可以用很多方式來定義,對於一些以前從未做過的人來說,對於某些專家來說很簡單。然而,這是Aditya鏈接的自定義UICollectionViewFlowLayout的示例。 – 2017-09-03 08:20:19

回答

0

我相信你可以做到這一點最簡單的方法是繼承UICollectionViewFlowLayout並作出改變的屬性,它不能的viewController內完成。因爲我剛剛寫了一個類似的自定義佈局來做到這一點,使它成爲一個吊艙,也許它(github link)可以提供幫助。