作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用的是相对简单的组合布局,它确实有效,但在某些情况下,我的 UICollectionView 中的最后一两个单元格的宽度似乎不正确。
我的 collectionView 由具有动态宽度和静态高度的单元格组成。我可以预先计算高度,因为我的单元格基本上是 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 上的模拟器中运行,但是当我尝试在较小的屏幕(在我的例子中是与 iPhone 兼容的第 3 代 iPad Air)上模拟时,这个 collectionView 似乎被破坏了。
在这种情况下,最后一两个单元格的宽度变窄了,我不知道为什么:
这里发生了什么?
最佳答案
让 estimatedWidth: CGFloat = 200把它改成 50 就可以了
关于swift - 组合布局和 splinter 的自定大小的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61497430/
我正在实现谱聚类算法,我必须确保矩阵(拉普拉斯矩阵)是半正定矩阵。 检查矩阵是否为正定矩阵 (PD) 就足够了,因为可以在特征值中看到“半”部分。矩阵非常大(nxn,其中 n 是几千的数量级)所以特征
我是一名优秀的程序员,十分优秀!