CollectionViewCell 保证金?
CollectionViewCell margin?
我有一个 UICollectionView
并且其中有一些已出列的自定义单元格。正如您从屏幕截图中看到的那样,有一个奇怪的边距,我不知道这是从哪里来的。 brown/orange 颜色是 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
}
您的 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;
}
我有一个 UICollectionView
并且其中有一些已出列的自定义单元格。正如您从屏幕截图中看到的那样,有一个奇怪的边距,我不知道这是从哪里来的。 brown/orange 颜色是 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
}
您的 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;
}