Я использую относительно простую композиционную компоновку, и она работает, но в некоторых случаях последние одна или две ячейки в моем UICollectionView, кажется, имеют ломаную ширину.
Мой collectionView состоит из ячеек с динамичностью c ширина и высота c высота. Я могу рассчитать высоту заранее, потому что моя ячейка в основном UILabel плюс фоновый UIView плюс верхние / левые / правые / нижние ограничения (каждая с UIPriority (1000)).
topicBackView = UIView()
topicBackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(topicBackView)
topicLabel = MyLabel()
topicLabel.translatesAutoresizingMaskIntoConstraints = false
topicLabel.numberOfLines = 0
topicLabel.font = cs.usualFont
topicLabel.textColor = UIColor.black
topicLabel.textAlignment = .center
contentView.addSubview(topicLabel)
let tpp = topicBackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0)
tpp.priority = UILayoutPriority(1000)
tpp.isActive = true
и т. Д.
Я знаю шрифт для UILabel, я знаю все константы ограничений, поэтому легко предсказать высоту ячейки.
Из-за этого мой макет выглядит так:
func giveMeFreeTagsLayout() -> UICollectionViewLayout {
let estimatedHeight: CGFloat = topicsCellCollectionHeight
let estimatedWidth: CGFloat = 200
let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(estimatedWidth),
heightDimension: .absolute(estimatedHeight))
//height is absolute because I know it, and in some cases-not in this one, though-I have to calculate the height of UICollectionView
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.edgeSpacing = NSCollectionLayoutEdgeSpacing(leading: .fixed(0), top: .fixed(8), trailing: .fixed(8), bottom: .fixed(8))
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(estimatedHeight))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,
subitems: [item])
group.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 20, trailing: 0)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
Дело в том, что он работает в симуляторе на iPhone 11, но этот collectionView, кажется, не работает, когда я пытаюсь эмулировать это на экранах меньшего размера (в моем случае, iPad Air 3-го поколения с iPhone совместимость).
В этом случае одна или две последние ячейки имеют ломаную ширину, и я не знаю, почему:
![Bad one](https://i.stack.imgur.com/oi9ad.png)
Как и должно быть:
Что здесь происходит?