【发布时间】:2017-11-15 09:46:36
【问题描述】:
我有一个UICollectionView,其中有一些已出列的自定义单元格。正如您从屏幕截图中看到的那样,有一个奇怪的边距,我无法弄清楚这是从哪里来的。棕色/橙色是UICollectionView,灰色是UICollectionViewCell。
我正在为该单元格使用 xib 文件,因为我需要许多自定义单元格。
生成UICollectionView 的代码没有任何东西会导致这个边距。
extension DashVC: UICollectionViewDelegate, UICollectionViewDataSource
{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PostImageCell", for: indexPath) as! PostImageCell
let post = posts[indexPath.row]
let user = users[indexPath.row]
cell.post = post
cell.user = user
cell.delegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
func setCollectionViewDelegatesAndRowHeight()
{
collectionView.dataSource = self
collectionView.delegate = self
}
}
更新:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let width = Int(UIScreen.main.bounds.width)
let height = 407
let size = CGSize(width: width, height: height)
return size
}
【问题讨论】:
-
单元格的大小与collectionview的宽度不同!提供collectionViewCell的大小等于collectionview的宽度!
-
你说的是哪个边距?您的屏幕截图上有几个。
-
@SaurabhPrajapati 我尝试使用
let width = Int(collectionView.frame.width),但它仍然不起作用。边距保持
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout