简单地使用故事板,约束来设置 UICollectionView 中的单元格大小
Simply using storyboard, constraints to set size of cell in UICollectionView
这是一个 UICollectionView,紫色的单元格:
很简单,我希望单元格为集合视图宽度的 1/2。 (所以待定,集合视图中的单元格将排列成两行。)
(集合视图只是全屏,所以每个单元格都是屏幕宽度的一半。)
你如何在情节提要中做到这一点?
如果我尝试以正常方式控制拖动,它基本上不起作用。
这些是简单的完全静态单元格(不是动态的)。
对于在这里谷歌搜索的任何人,为了节省您的时间:这正是(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/2 宽度”的概念。 (如果你事先知道集合视图的宽度,你可以使用故事板中的流布局来绝对设置单元格大小,通过在你的头脑中划分;但你不知道,因为宽度因设备而异.)
这是一个 UICollectionView,紫色的单元格:
很简单,我希望单元格为集合视图宽度的 1/2。 (所以待定,集合视图中的单元格将排列成两行。)
(集合视图只是全屏,所以每个单元格都是屏幕宽度的一半。)
你如何在情节提要中做到这一点?
如果我尝试以正常方式控制拖动,它基本上不起作用。
这些是简单的完全静态单元格(不是动态的)。
对于在这里谷歌搜索的任何人,为了节省您的时间:这正是(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/2 宽度”的概念。 (如果你事先知道集合视图的宽度,你可以使用故事板中的流布局来绝对设置单元格大小,通过在你的头脑中划分;但你不知道,因为宽度因设备而异.)