【问题标题】:CollectionViewCell's container view doesn't fill cellCollectionViewCell 的容器视图未填充单元格
【发布时间】:2019-09-21 01:01:48
【问题描述】:

我有一个collectionView,它的高度和宽度是我使用自动布局设置的约束。 在我的

func collectionView(_ collectionView: ...., sizeForItemAt indexPath: IndexPath)我返回CGSize(width: self.collectionView.frame.width-20, height: self.collectionView.frame.height),如下图所示,它排列得非常好。

问题是该单元格的容器视图(空白区域)根本没有填满绿色空间(即单元格的背景),即使我已将 containerView 的约束设置为拥抱左/右/下/up 0 边距。

到目前为止我所尝试的

  • 为我分配集合视图高度的容器视图进行高度约束

  • updateConstraints()cellForItemAt 中以及在```layoutSubview()```` 中的单元格类中

  • 将容器视图的框架设置为collectionView的框架

  • cell.layoutIfNeeded()in cellForItemAt

这些都没有解决我的问题。

https://i.stack.imgur.com/mD2iC.jpg

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    我认为这里的问题与插图有关。请使用集合委托方法

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
        return UIEdgeInsetsMake(top, left, bottom, right);
    
    }
    

    UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    [aFlowLayout setSectionInset:UIEdgeInsetsMake(top, left, bottom, right)];
    

    斯威夫特 5.1

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15.0
    }
    

    【讨论】:

    • @FlatMorning 请添加以下方法一次 - (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 15.0; }
    • 感谢您的回复,但您似乎正在使用 objc 代码进行回复。我的问题是针对 Swift 的。
    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 2019-07-11
    • 1970-01-01
    • 2020-05-19
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    相关资源
    最近更新 更多