3

我希望所有單元格之間以及屏幕邊框和最近的單元格之間具有相等的間距。基本上是這樣的。等距和周圍。UICollectionViewCells周圍的等間距

enter image description here

我設法通過這樣的設定值來測試這個在界面生成器。我設置了單元尺寸爲110,敏間距爲0,所有科插圖分至10

enter image description here

它看起來完美的IB。

enter image description here

然而,當我運行它(在iPad上航2),它看起來像這樣。

enter image description here

通知在小區之間的間距較大。我還試圖通過從UICollectionViewDelegateFlowLayout實施以下方法完全從代碼執行此操作。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSize(width: 110, height: 110) 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
    return 0 
} 

這產生了相同的結果。任何人都可以告訴我如何糾正這個問題嗎?我寧願通過代碼來做到這一點。但我對任何事情都是開放的。

回答

1

不幸的是,這是默認情況下UICollectionViewFlowLayout的工作方式。這是因爲您指定了精確的部分插頁,但是最小的插頁空間。 要做你正在問的問題,你主要有兩種方法來做到這一點,取決於你想要的最終結果。兩者都必須由代碼手動完成。

•放大的細胞離開確切的空間(同一空間,更大的細胞)

•計算空間,改變插圖與之匹配(相同的電池,更大的空間)

既可以做通過設置佈局對象上的值(並使其無效)或通過代理返回值。