Instagram / IGListKit

A data-driven UICollectionView framework for building fast and flexible lists.
https://instagram.github.io/IGListKit/
MIT License
12.87k stars 1.54k forks source link

Grid Layout #579

Closed thedevme closed 7 years ago

thedevme commented 7 years ago

I have not been able to find a solution for this as all the examples have the grid flowing to the left. I need to get my grid to be centered. Currently it is floating to the left:

img_1909

I am not sure why my values for inset are not working but they are not in the SectionController. Any advice? Thanks

zhubofei commented 7 years ago

@thedevme The grid layout only supports left alignment at the time. For your use case, I think UICollectionViewFlowLayout might be more suitable (if you only have one Section Controller per line).

thedevme commented 7 years ago

I am not sure what you mean - so are you saying use reg collection view for this? I will need it to be centered.

zhubofei commented 7 years ago

I mean use UICollectionViewFlowLayout instead of IGListCollectionViewLayout.

thedevme commented 7 years ago

Ahh ok thank you very much for the quick response

thedevme commented 7 years ago

@zhubofei does this also apply to a grid?

screen shot 2017-03-23 at 2 29 42 pm

I tried using UICollectionViewFlowLayout but it won't show up in a grid - when I use IGListCollectionViewLayout I get the following. But I need the following:

img_1910

zhubofei commented 7 years ago

@thedevme No. UICollectionViewFlowLayout newlines section controllers. If you want grid layout and center alignment at the same time. We have to do the math yourself. Setup the right width and spacing.

thedevme commented 7 years ago

I have tried with both ways doing it with math and setting the width and it still doesn't thought it might be a bug. Thanks

rnystrom commented 7 years ago

@thedevme can you use IGListCollectionViewLayout and set the contentInset of the collection view itself to something to "center" the content?

D-Link13 commented 7 years ago

Same thing here.. If I am using Flow layout, insets works like a charm.. but when using Grid, settings the insets has no effect. Here is my code:

import UIKit
import IGListKit

class InviteFriendsSectionController: IGListSectionController {

    var item: String!
    let spacing = CGFloat(20)

    override init() {
        super.init()
        minimumLineSpacing = spacing
        minimumInteritemSpacing = spacing
        inset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
    }
}

extension InviteFriendsSectionController: IGListSectionType {

    func numberOfItems() -> Int {
        return 1
    }

    func sizeForItem(at index: Int) -> CGSize {

        guard let context = collectionContext else { return .zero }
        let width = context.containerSize.width / 2 - spacing * 2

        return CGSize(width: width, height: width)
    }

    func cellForItem(at index: Int) -> UICollectionViewCell {

        let cell = collectionContext?.dequeueReusableCellFromStoryboard(withIdentifier: "InviteFriendCell", for: self, at: index) as! InviteFriendCell
        cell.imageView.image = UIImage(named: item)
        return cell
    }

    func didUpdate(to object: Any) {
        item = object as? String
    }

    func didSelectItem(at index: Int) {

    }
}

Help me please!

rnystrom commented 7 years ago

@D-Link13 can you try using the master branch and our IGListCollectionViewLayout layout instead? We've deprecated the grid layout in favor of an upgraded one that better supports insets and inline sections/items.

If you're still having trouble please open a new issue so we can track there!