2017-08-11 9 views
0

コレクションビューに複数の画像が正常に動作していました。それはこのコレクションビューセルHight and width in swift

enter image description here

のように見せているが、私はこの方法のように表示したい

enter image description here

これはコレクションビューセル

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 20, right: 0) 
let width = UIScreen.main.bounds.width 
layout.itemSize = CGSize(width: width/2 , height: width/2) 
layout.minimumInteritemSpacing = 0 
layout.minimumLineSpacing = 0 
collectionView!.collectionViewLayout = layout 

答えて

1

を試してみてください。

例:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
{ 
    let cellSize = CGSize(width: (collectionView.bounds.width - (3 * 10))/2, height: 120) 
    return cellSize 
} 

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets 
{ 
    let sectionInset = UIEdgeInsetsMake(10, 10, 10, 10) 
    return sectionInset 
} 

ちょうどあなたの要件に応じて大きさや間隔の値を変更します。

あなたは、コレクションビューのセル&ラインの最小間隔を設定し、怒鳴るデリゲートを実装しようとすることができます
2

のコードは、あなたのViewController

にこのメソッドを追加しています
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { 
     return CGSize(width: ((self.view.frame.size.width/2) - 10), height: 255) 
    } 
0

ただUICollectionViewCellsの正しいサイズと間隔を得るために、いくつかのUICollectionViewDelegateFlowLayoutのメソッドを実装し、

collectionViewCellImageView.contentMode = .scaleToFill 
0

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