具有估计高度的集合视图组合布局不起作用 [英] Collection View Compositional Layout with estimated height not working
问题描述
我希望我的应用针对包括文本大小在内的每个辅助功能选项进行优化.
I want my app to be optimized for every accessibility options including the text size.
我基于具有组合布局的部分制作了一个 collectionView 布局.所以我需要我的细胞的高度随着它的内容而增长.我认为使用 .estimated(constant)
可以完成这项工作,但它似乎不起作用.内部约束对我来说似乎很好.
I made a collectionView layout based on sections with a compositional layout. So I need my cell's height to grow with it's content. I thought using .estimated(constant)
would do the job but it doesn't seem to work. The inner constraints seems good to me.
这是我正在使用的布局:
Here is the layout I'm working with :
let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.42), heightDimension: .estimated(90))
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(90)))
item.contentInsets = NSDirectionalEdgeInsets(top: 5.0, leading: 12.0, bottom: 5.0, trailing: 12.0)
let group = NSCollectionLayoutGroup.vertical(layoutSize: size, subitem: item, count: 1)
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 0.0, leading: 6.0, bottom: 0.0, trailing: 0.0)
section.orthogonalScrollingBehavior = .groupPaging
当我在可访问性设置上设置更高的文本大小时,会发生以下情况:
When I set a higher text size on the accessibility settings here is what happens :
单元格应该包含 2 个标签,这里是 autoLayoutConstraints :
The cell is supposed to contain 2 labels here is the autoLayoutConstraints :
NSLayoutConstraint.activate([
self.titleLabel.topAnchor.constraint(equalTo: self.container.topAnchor, constant: 10),
self.titleLabel.leftAnchor.constraint(equalTo: self.container.leftAnchor, constant: 20),
self.titleLabel.rightAnchor.constraint(equalTo: self.container.rightAnchor, constant: -20)
])
NSLayoutConstraint.activate([
self.subtitleLabel.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: 10),
self.subtitleLabel.leftAnchor.constraint(equalTo: self.container.leftAnchor, constant: 20),
self.subtitleLabel.rightAnchor.constraint(equalTo: self.container.rightAnchor, constant: -20),
self.subtitleLabel.bottomAnchor.constraint(equalTo: self.container.bottomAnchor, constant: -10)
])
提前感谢您的帮助.
推荐答案
我发现让我的 LayoutGroup 水平而不是垂直解决了这个问题.这是我的最终布局:
I found that making my LayoutGroup horizontal instead of vertical fixed the issue. Here is my final layout :
let estimatedHeight = CGFloat(100)
let layoutSize = NSCollectionLayoutSize(widthDimension: .estimated(200), heightDimension: .estimated(estimatedHeight))
let item = NSCollectionLayoutItem(layoutSize: layoutSize)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: layoutSize, subitem: item, count: 1)
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
section.interGroupSpacing = 10
section.orthogonalScrollingBehavior = .groupPaging
希望对你有所帮助^^
这篇关于具有估计高度的集合视图组合布局不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!