Open PhilCai1993 opened 7 years ago
After checking StackOverFlow This can be solved
final class ManuallySelfSizingCell/FullWidthSelfSizingCell/NibSelfSizingCell: UICollectionViewCell {
var hasCalculated = false
......
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
if hasCalculated {
return layoutAttributes
}
hasCalculated = true
setNeedsLayout()
layoutIfNeeded()
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
var newFrame = layoutAttributes.frame
// note: don't change the width
newFrame.size.height = ceil(size.height)
layoutAttributes.frame = newFrame
return layoutAttributes
}
}
This can be solved on iOS 9. But on iOS 10 the layout becomes strange. I don't know what about iOS 8 or even iOS 7. Anyway, this is not a bug in IGListKit. @rnystrom
Thanks for the update @PhilCai1993 ! 😄
I'm currently using self sizing cells with IGListKit in a client app and had this crash. I tracked it down to unneeded calls to:
setNeedsLayout()
layoutIfNeeded()
in preferredLayoutAttributesFitting. I wasn't able to reproduce the issue in the demo app, but you might want to try removing those methods.
I think it makes sense that these calls aren't needed by systemLayoutSizeFitting to calculate the size.
@jeffbailey good to know. I should try this out on iOS 9/10, maybe even put a version check in there if the behavior changes. Did you hit this crash on 10?
FYI I pasted the solution from http://stackoverflow.com/a/25896386/940936 and removed the caching bit that @PhilCai1993 called out as fixing iOS 9 b/c I ran into some weird behavior on 10.
Yes, the crash was on 10, but went away as soon as I removed those lines. I believe it had to do with those methods being called on cells that were about to be reused, but that's just a guess.
@jeffbailey if you run into it again or are able to make the example project do this I'd love to take a deeper look. I'll play around w/ different setups to see what works best here in the meantime.
How to solve this crash on 10?
@sun-fox-cj have you given @jeffbailey's solution a try?
I have solved this problem without version check. Before I fix this crash, I did it as follows:
layoutIfNeeded
and systemLayoutSizeFitting
method to calculate the height of content view, then cache it. It works well before iOS10, but
crashes in iOS10.
So I change it like this: I add inner UIControls constraint with contentView to certain the width of contentView instead of add a width constraint directly. It works well, again.
@rnystrom I'm getting a jerky animation when I'm using the searchBar and self-sizing cells. Have you seen this before? It's happening on iOS 10
I'm still getting crash on iOS 9.3 in Example iOS App (Self-sizing cells).
Self-sizing cells crash bug
General information
IGListKit
version: latest on master