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

Vertical Aligment Bug #12

Open
WessonWu opened this issue Jun 23, 2018 · 4 comments
Open

Vertical Aligment Bug #12

WessonWu opened this issue Jun 23, 2018 · 4 comments

Comments

@WessonWu
Copy link

WessonWu commented Jun 23, 2018

    fileprivate func layoutAttributes(forItemsInLineWith layoutAttributes: UICollectionViewLayoutAttributes) -> [UICollectionViewLayoutAttributes] {
        guard let lineWidth = contentWidth else {
            return [layoutAttributes]
        }
        var lineFrame = layoutAttributes.frame
        lineFrame.origin.x = sectionInset.left
        lineFrame.size.width = lineWidth
        
        // Need to be add
        // bug fix for super.layoutAttributesForElements(in:)
        // if there are three row (Row1, Row2, Row3), and rows step by step closly, such as
        // Row1: (x: 0, y: 0, width: 375, 100)
        // Row2: (x: 0, y: 100, width: 375, height: 100)
        // Row3: (x: 0, y: 200, width: 375, height: 100)
        // then super.layoutAttributesForElements() will reture 3 values(Row1, Row2, Row3), that is incorrect
        if lineFrame.height > 2 {
            lineFrame.origin.y += 1
            lineFrame.size.height -= 2
        }
        
        return super.layoutAttributesForElements(in: lineFrame) ?? []
    }
@mischa-hildebrand
Copy link
Owner

That's a good observation! Thanks for reporting this!

I hadn't noticed that it doesn't work with interitem / line spacings of 0.
However, this is not the only place where the frame needs to be adjusted:
More importantly, this function needs adjustment:
fileprivate func isFrame(for firstItemAttributes: UICollectionViewLayoutAttributes, inSameLineAsFrameFor secondItemAttributes: UICollectionViewLayoutAttributes)

Furthermore, x += 1 and height -= 2 won't do it as it will also introduce inaccuracy. Retina screens support 0.5pt intervals, super retina displays 1/3pt intervals and there might be other resolutions in the future. Thus, I'll need to find a solution how to deal with that in a safe way.

Any suggestions are welcome! :)

@mischa-hildebrand
Copy link
Owner

By the way:
I think, it's a rounding bug because the documentation of the intersects(_:) function itself states:

The first rectangle intersects the second if the intersection of the rectangles is not equal to the null rectangle.

When I define two rectangles as follows:

let rect1 = CGRect(x: 0, y: 0, width: 100, height: 100)
let rect2 = CGRect(x: 0, y: 100, width: 100, height: 100)

the intersection of the rectangles is the null rectangle and thus

rect1.intersects(rect2)

returns false.

@WessonWu
Copy link
Author

ok, I got it, thank you for your reply and correct my thinking

@yojkim
Copy link

yojkim commented Jul 1, 2021

Hi! Is there any solution for this problem?

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

3 participants