In a UICollectionView, the cells will not sit on the bottom

后端 未结 2 622
青春惊慌失措
青春惊慌失措 2020-12-19 19:54

Note it turned out it was a simple mistake the cells would not \"sit on the bottom\". However, it\'s an interesting question: \"how to move the cells up and

相关标签:
2条回答
  • 2020-12-19 20:49

    Here's one "brute force" way to solve the problem -- write your own FlowLayout!!

    It works ...

    class SimpleFlowLayout: UICollectionViewFlowLayout {
    
    
    override init() {
        super.init()
    
        initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }
    
    func initialize() {
        self.scrollDirection = .horizontal
        self.minimumInteritemSpacing = 0.0
        self.minimumLineSpacing = 0.0
    
    }
    
    override var collectionViewContentSize: CGSize {
    
        return super.collectionViewContentSize
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    
    
        let allItems = super.layoutAttributesForElements(in: rect)
        for  attribute in allItems! {
            print ("attribute.frame.origin.y  .....  \(attribute.frame.origin.y)")
            attribute.frame = CGRect(x: attribute.frame.origin.x,
                                     y: attribute.frame.origin.y + 34,
                                 width: attribute.frame.size.width,
                                height: attribute.frame.size.height)
    
                // "go figure" ... add 34
        }
    
        return allItems;
    
    }
    

    The way you "set" a UICollectionViewFlowLayout is basically like this...

    class YourFancyDisplay: UICollectionViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            collectionView!.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
            collectionView!.collectionViewLayout = SimpleFlowLayout()
            ...
    
    0 讨论(0)
  • 2020-12-19 20:52

    Try this. hopefully, it can help

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return collectionView.frame.size;
    }
    

    (For future readers, indeed you also typically need the other one ...)

    -(CGSize) collectionView:(UICollectionView *)collectionView
         layout:(UICollectionViewLayout *)collectionViewLayout
         sizeForItemAtIndexPath:(NSIndexPath *)indexPath
        {
        return self.view.frame.size;
        }
    
    -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
       layout:(UICollectionViewLayout*)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section
        {
        return UIEdgeInsetsMake(0,0,0,0);   //t,l,b,r
        }
    
    0 讨论(0)
提交回复
热议问题