子类化UICollectionViewFlowLayout-在iOS 12中自动调整单元格帧计算问题
我使用以下命令将子类化UICollectionViewFlowLayout-在iOS 12中自动调整单元格帧计算问题,ios,swift,uicollectionview,autosize,ios12,Ios,Swift,Uicollectionview,Autosize,Ios12,我使用以下命令将UICollectionView中的单元格左对齐 class LeftAlignedFlowLayout: UICollectionViewFlowLayout { override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { guard let attributes = super.layoutAttribute
UICollectionView
中的单元格左对齐
class LeftAlignedFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: rect) else { return nil }
for attributes in attributes where attributes.representedElementCategory == .cell {
attributes.frame = layoutAttributesForItem(at: attributes.indexPath).frame
}
return attributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes {
let currentItemAttributes = super.layoutAttributesForItem(at: indexPath)!
let layoutWidth = collectionView!.frame.width - sectionInset.left - sectionInset.right
// Just left align the first item in a section
if indexPath.item == 0 {
currentItemAttributes.frame.origin.x = sectionInset.left
return currentItemAttributes
}
let previousIndexPath = IndexPath(item: indexPath.item - 1, section: indexPath.section)
let previousItemFrame = layoutAttributesForItem(at: previousIndexPath).frame
let currentItemFrame = currentItemAttributes.frame
let wholeRowFrameForCurrentItem = CGRect(x: sectionInset.left, y: currentItemFrame.minY, width: layoutWidth, height: currentItemFrame.height)
// Left align the first item on a row
if !previousItemFrame.intersects(wholeRowFrameForCurrentItem) {
currentItemAttributes.frame.origin.x = sectionInset.left
return currentItemAttributes
}
currentItemAttributes.frame.origin.x = previousItemFrame.maxX + minimumInteritemSpacing
return currentItemAttributes, it's
}
}
iOS 11的构建提供了
但是对于iOS 12来说
在iOS 11中,layouttributesforements(In)
被调用3次,这是帧大小正确的最后一次调用。在iOS 12中,它只被调用两次,帧是估计的大小
根据这个答案,我已经尝试使用下面的UICollectionView
子类(以及包含视图控制器的viewdide
中的“暴力强制”)使流布局无效,但结果没有改变
class LeftAlignedCollectionView: UICollectionView {
private var shouldInvalidateLayout = false
override func layoutSubviews() {
super.layoutSubviews()
if shouldInvalidateLayout {
collectionViewLayout.invalidateLayout()
shouldInvalidateLayout = false
}
}
override func reloadData() {
shouldInvalidateLayout = true
super.reloadData()
}
}
这是iOS 12中的一个已知问题(请参阅此处的UIKit部分:)
解决方法是在调用
systemLayoutSizeFitting(u:)
之前,避免调用setNeedsUpdateConstraints()
或调用updateconstraintsifneeds()
,我找到了一个解决方案。将contentView
约束到单元格修复了它
override func awakeFromNib() {
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([contentView.leftAnchor.constraint(equalTo: leftAnchor),
contentView.rightAnchor.constraint(equalTo: rightAnchor),
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
}
我在MyCustomCollectionViewCell文件中添加了这个,效果很好
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: 1))
}
我也有这个问题,你找到解决办法了吗?@EnasAZ我找到了!见下面的答案