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

Collection view header getting overlapped #40

Open
nadeemworkspace opened this issue Jun 16, 2024 · 1 comment
Open

Collection view header getting overlapped #40

nadeemworkspace opened this issue Jun 16, 2024 · 1 comment

Comments

@nadeemworkspace
Copy link

Collection view header getting overlapped while setting
let alignedFlowLayout = collectionView?.collectionViewLayout as? AlignedCollectionViewFlowLayout
alignedFlowLayout?.horizontalAlignment = .left
alignedFlowLayout?.verticalAlignment = .top
Simulator Screenshot - iPhone 15 Plus - 2024-06-16 at 21 17 17

@iosdec
Copy link

iosdec commented Jul 29, 2024

The issue is that verticalAlignmentAxisForLine is considering the supplementary views with the cells. Apply these changes below, and everything should work as expected.

private func verticalAlignmentAxisForLine(
        with layoutAttributes: [UICollectionViewLayoutAttributes]
    ) -> AlignmentAxis<VerticalAlignment>? {

        let cellAttributes = layoutAttributes.filter {

            return $0.representedElementCategory == .cell
        }

        guard let firstAttribute = cellAttributes.first
        else {

            return nil
        }

        let indexPath = firstAttribute.indexPath

        switch verticalAlignment {

        case .top:
            let minY = cellAttributes.reduce(CGFloat.greatestFiniteMagnitude) {
                min($0, $1.frame.minY)
            }

            return AlignmentAxis(
                alignment: .top,
                position: minY
            )

        case .bottom:
            let maxY = cellAttributes.reduce(0) {
                max($0, $1.frame.maxY)
            }

            return AlignmentAxis(
                alignment: .bottom,
                position: maxY
            )

        default:
            let centerY = firstAttribute.center.y

            return AlignmentAxis(
                alignment: .center,
                position: centerY
            )
        }
    }
fileprivate func isFrame(
        for firstItemAttributes: UICollectionViewLayoutAttributes,
        inSameLineAsFrameFor secondItemAttributes: UICollectionViewLayoutAttributes
    ) -> Bool {

        guard let lineWidth = contentWidth 
        else
        {
            return false
        }
        let firstItemFrame = firstItemAttributes.frame

        let lineFrame = CGRect(
            x: sectionInset.left,
            y: firstItemFrame.origin.y,
            width: lineWidth,
            height: firstItemFrame.size.height
        )

        return lineFrame.intersects(
            secondItemAttributes.frame
        )
    }
open override func layoutAttributesForSupplementaryView(
        ofKind elementKind: String,
        at indexPath: IndexPath
    ) -> UICollectionViewLayoutAttributes? {

        let attributes = super.layoutAttributesForSupplementaryView(
            ofKind: elementKind,
            at: indexPath
        )

        return attributes
    }
private func setFrame(
        forLayoutAttributes layoutAttributes: UICollectionViewLayoutAttributes
    ) {

        if layoutAttributes.representedElementCategory == .cell { // Do not modify header views etc.

            let indexPath = layoutAttributes.indexPath

            if let newFrame = layoutAttributesForItem(
                at: indexPath
            )?.frame {

                layoutAttributes.frame = newFrame
            }
        }

        if layoutAttributes.representedElementKind == UICollectionView.elementKindSectionHeader {

            let indexPath = layoutAttributes.indexPath

            if let newFrame = layoutAttributesForSupplementaryView(
                ofKind: UICollectionView.elementKindSectionHeader,
                at: indexPath
            )?.frame {

                layoutAttributes.frame = newFrame
            }
        }
    }

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

2 participants