我有一个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
}
答案 0 :(得分:2)
UICollectionView
宽度为375,UICollectionViewCell
宽度为320.您看到了差异。要使单元格与屏幕一样宽,可以使控制器符合UICollectionViewDelegateFlowLayout
,并实现以下方法:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
cellHeight = 407.0
CGSize size = CGSizeMake(screenWidth, cellHeight);
return size;
}