我正在使用以下命令将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中,layoutAttributesForElements(in)
被调用3次-最终时间为正确的帧大小。在iOS 12中,它只被调用了两次,帧是估计的大小。
按照这个答案,https://stackoverflow.com/a/52148520/123632我尝试使用下面的UICollectionView
子类(和包含视图控制器的viewDidAppear
中的“蛮力”)使流布局无效,但是结果不变。
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()
}
}
答案 0 :(得分:2)
这是iOS 12中的一个已知问题(请参见https://developer.apple.com/documentation/ios_release_notes/ios_12_release_notes的UIKit部分)
解决方法是避免调用setNeedsUpdateConstraints()
之前先调用updateConstraintsIfNeeded()
或先调用systemLayoutSizeFitting(_:)
。
答案 1 :(得分:1)
我找到了解决方案。将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)])
}
答案 2 :(得分:0)
我将此添加到MyCustomCollectionViewCell文件中,并且效果很好
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: 1))
}