2016-07-13 26 views
0

UICollectionViewControllerを作成し、各行に3つのセルを表示します。各セルの幅は画面全体の1/3でなければなりません。しかし、最初にUICollectionViewControllerシーンに入ると、すべてのセルが非常に小さくなります。私が画面をスクロールすると、いくつかのセルが予想されるサイズの大きなセルになります。 enter image description here enter image description here enter image description here画面をスクロールすると、UICollectionViewCellのサイズが異なります

私のコードの一部:

class imageCollectionViewController: UICollectionViewController { 
private let reuseIdentifier = "photoCell" 
var photos:albumCellModel? 
override func viewDidLoad() { 
    super.viewDidLoad() 
    navigationController?.toolbarHidden = true 

    if let layout = collectionView!.collectionViewLayout as? UICollectionViewFlowLayout { 
     let itemWidth = view.bounds.width/3.0 
     let itemHeight = layout.itemSize.height 
     layout.itemSize = CGSize(width: itemWidth, height: itemHeight) 
     layout.invalidateLayout() 
    } 
} 
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 

    return 1 
} 
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if let photos = photos 
    { 
     return photos.photoSet.count 
    } 
    return 0 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("photoCell", forIndexPath: indexPath) as! imageCollectionViewCell 
    cell.imageView.image = photos?.photoSet[indexPath.row].image 
    return cell 
} 

答えて

1

私は私の以前のプロジェクトと同じ問題が発生しません。私はこのライブラリを使用して問題を解決することができました。 Snapkit。ポッド内のライブラリをインポートし、cellForItemAtIndexPath

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! TestCell 
let width = self.view.bounds.size.width/2 - 10 
    cell.imageView.snp_makeConstraints { (make) -> Void in 
     make.width.equalTo(width) 
     make.height.equalTo(3*width/2) 
    } 

return cell 

} 

だけ置くだけで、トップにそれを実装し、ストーリーボードでImageViewの上の主要な制約。それが役に立てば幸い。

+0

私はあなたが甘んじて2つの制約を削除するだけですが、その結果、それは私のイメージの一部を表示することになります。 – beasone

+0

私はポッドに提案するライブラリ –

+0

をインストールしました申し訳ありませんが、私はそのライブラリをインストールしていません。私は今それを試してみます。 – beasone

関連する問題