Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work properly with auto layout cell sizing #5

Closed
damienrambout opened this issue Apr 23, 2015 · 6 comments
Closed

Does not work properly with auto layout cell sizing #5

damienrambout opened this issue Apr 23, 2015 · 6 comments

Comments

@damienrambout
Copy link

When I set a estimatedItemSize on the collection view layout and use auto-layout to define the size of my cells (hence, not implementing - (CGSize)collectionView:layout:sizeForItemAtIndexPath:), KTCenterFlowLayout does not work anymore.

Since iOS 8, it's very usual to use auto-layout for UICollectionViewCell automatic sizing (as well as UITableViewCell automatic height).

@keighl
Copy link
Owner

keighl commented Apr 23, 2015

Hey @damienrambout. Thanks for that. I'm not familiar with what UICollectionViewFlowLayout does under the hood exactly in that case.

This class just hijacks the layoutAttributesForElementsInRect method on flow layout. I would sort of expect all the sizing to be determined at that point (auto-layout or otherwise). But there are probably differences that we need to account for.

I will investigate when I can. Feel free to dig deeper!

@bsantanas
Copy link

Thank you very much! I spent the last two days trying to figure out why my swift implementation wasn't working and now I see this post... I came to the exact same conclusion that @damienrambout pointed out. If you come up with something please let me know! I'll do the same.
Cheers!

@thinalgal
Copy link

One solution, which is not efficient but works, is to use the existing implementation of layoutAttributesForElementsInRect to implement layoutAttributesForItemAtIndexPath, by defining a rect that grabs all elements and then just picking out the one you want using its indexPath, as follows:

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        let modifiedLayoutAttributes = self.layoutAttributesForElementsInRect(CGRectInfinite)

        if let desiredLayAttr = modifiedLayoutAttributes?.filter({ indexPath.isEqual($0.indexPath) }).first as? UICollectionViewLayoutAttributes {
            return desiredLayAttr
        }
        else
        {
            NSLog("error")
            return super.layoutAttributesForItemAtIndexPath(indexPath)
        }
    }

@niklassaers
Copy link

Hi guys, I am running into the same issue with auto-layout based cells, but I believe I have come a good way at hunting this down.

When I am laying it out, I don't get the nice spacing I used to before individual cell sizing, I can get to getting nice spacing if I in

func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes

just return layoutAttributes. However, when I have a implementation that returns the size I want, the layout collapses to this:

screenshot 2016-02-03 12 27 43

The really interesting thing is that if I rotate, and then rotate back, I get the expected look:

screenshot 2016-02-03 12 27 54

You can find the project here: https://github.com/skylib/SnapTagsView/tree/DemoForKTCenterFlowLayout and the function referred to above lives in SnapTagsView/SnapTagCell.swift. Just run the example app in it and you'll see exactly what I mean. (you'll need to do a pod install before compiling it)

Cheers

@keighl
Copy link
Owner

keighl commented Feb 3, 2016

thanks @niklassaers i'll check it out

@iliaskarim
Copy link

I'm also noticing that this layout class does not seem to work with auto sizing collection view cells.

Anyone know any alternatives? 😅 I'll probably just be left-aligning for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants