2

这里有一个UICollectionView,紫色细胞:只需用故事板,制约设置单元格的大小UICollectionView

enter image description here

很简单,我想细胞是集合视图的1/2宽度。 (所以TBC,这将是在集合视图细胞的两行排列。)

(集合视图仅仅是全屏,让每一个细胞都在屏幕宽度的一半。)

你如何做到这一点在故事板?

如果我试图以正常方式控制拖动,它基本上不起作用。

这些都是简单的完全静态单元格(不是动态的)。


对于谷歌搜索的人在这里,节省您的时间:这正是(2016),使两跨UICollectionView布局最简单的方法;细胞之间没有空隙。

// Two - two-across UICollectionView 
// use a completely standard UIViewController on the storyboard, 
// likely change scroll direction to vertical. 
// name the cell identifier "cellTwo" on the storyboard 
import UIKit 
class Two:UICollectionViewController 
    { 
    override func viewDidLoad() 
     { 
     super.viewDidLoad() 

     let w = collectionView!.bounds.width/2.0 

     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
     layout.itemSize = CGSize(width:w,height:w) 
     layout.minimumInteritemSpacing = 0 
     layout.minimumLineSpacing = 0 

     collectionView!.collectionViewLayout = layout 

     // Note!! DO NOT!!! register if using a storyboard cell!! 
     // do NOT do this: 
     // self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 
     } 

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int 
     { return 1 } 
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
     { return 5 } 
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
     { 
     return collectionView.dequeueReusableCellWithReuseIdentifier("cellTwo", forIndexPath: indexPath) 
     } 
    } 
+1

看到这个答案:http://stackoverflow.com/a/38028456/1630618 – vacawama

回答

4

你不能在故事板中做到这一点。集合视图宽度直到运行时才知道,并且集合视图单元格不在自动布局下,因此您无法表达其他任何内容的“1/2宽度”概念。 (如果您事先知道收集视图宽度,则可以使用故事板中的流布局来绝对地设置单元格大小,方法是将头部分开;但您不知道,因为宽度因设备而异)

+0

明白了,谢谢你的解释。 – Fattie

相关问题