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

Crash on iPad for iOS 15 #35

Open
chamitha opened this issue Sep 2, 2021 · 4 comments
Open

Crash on iPad for iOS 15 #35

chamitha opened this issue Sep 2, 2021 · 4 comments

Comments

@chamitha
Copy link

chamitha commented Sep 2, 2021

I'm getting a crash force unwrapping a nil value testing on iOS 15 Beta but on iPad only. iPhone seems to work fine. The crash occurs here

// It's okay to force-unwrap here because we pass a non-empty array.
return verticalAlignmentAxisForLine(with: layoutAttributesInLine)!

the cause being on iPad
super.layoutAttributesForElements called by layoutAttributes(forItemsInLineWith: currentLayoutAttributes) returns nil (and therefore an empty array) whereas on iPhone this returns the layout attributes of the cell. The verticalAlignmentAxisForLine func returns nil if the attributes array is empty therefore causing the force unwrap to crash.

guard let firstAttribute = layoutAttributes.first else {
  return nil
}

Whilst this issue might be fixed in a subsequent beta release I think it exposes a valid issue with the force unwrap. Perhaps a default alignment should be returned by verticalAlignmentAxisForLine instead of nil?

guard let firstAttribute = layoutAttributes.first else {
  return AlignmentAxis(alignment: verticalAlignment, position: 0)
}
@JAGUAR108
Copy link

@chamitha I faced the same problem, please try using alignedFlowLayout?.verticalAlignment = .center

@chamitha
Copy link
Author

chamitha commented Sep 8, 2021

@chamitha I faced the same problem, please try using alignedFlowLayout?.verticalAlignment = .center

Thanks @JAGUAR108 but I need the vertical alignment to be top.

@CocoaBob
Copy link

CocoaBob commented Oct 7, 2021

I changed the following method to return at least 1 layout attribute, and it looks like it's working.

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
    let result = super.layoutAttributesForElements(in: lineFrame)
    if let result = result, !result.isEmpty {
        return result
    } else {
        return [UICollectionViewLayoutAttributes()]
    }
}

VladMelnik added a commit to VladMelnik/AlignedCollectionViewFlowLayout that referenced this issue Dec 30, 2021
@way2dipak
Copy link

I changed the following method to return at least 1 layout attribute, and it looks like it's working.

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
    let result = super.layoutAttributesForElements(in: lineFrame)
    if let result = result, !result.isEmpty {
        return result
    } else {
        return [UICollectionViewLayoutAttributes()]
    }
}

working.... 💯 🔥

FrancescoTr added a commit to FrancescoTr/AlignedCollectionViewFlowLayout that referenced this issue Jul 29, 2022
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

4 participants