2017-08-11 121 views
0

集合視圖有多個圖像正常工作。它顯示像這樣集合視圖單元格中的高度和寬度

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

只是執行一些UICollectionViewDelegateFlowLayout方法來獲取UICollectionViewCells的正確大小和間距。

實施例:

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

嘗試,

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) 
    } 
相關問題