gpt4 book ai didi

ios - 具有估计高度的 Collection View 组合布局不起作用

转载 作者:行者123 更新时间:2023-12-01 15:21:19 24 4
gpt4 key购买 nike

我希望我的应用针对包括文本大小在内的每个可访问性选项进行优化。

我根据具有组合布局的部分制作了一个 collectionView 布局。所以我需要我的细胞的高度随着它的内容而增长。我想用 .estimated(constant)会做这项工作,但它似乎不起作用。内部约束对我来说似乎很好。

这是我正在使用的布局:

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

当我在可访问性设置上设置更高的文本大小时,会发生以下情况:

enter image description here

该单元格应该包含 2 个标签,这里是 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)
])

在此先感谢您的帮助。

最佳答案

为我解决问题的是为项目和组设置相同的高度尺寸 .

我知道问题的共享代码就是这种情况,@ Que20 的解决方案肯定有帮助。但如果问题仍然存在,您可能需要检查一下。

例子

我正在尝试显示一个包含单个 UIButton 的单元格,该 UIButton 固定在单元格的边缘。该按钮具有 44 点高度限制。

最初的尝试

static func mapSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .fractionalHeight(1)) // Here: a fractional height to the item
let item = NSCollectionLayoutItem(layoutSize: itemSize)

let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: .estimated(100)) // There: estimated height to the button cell
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

let section = NSCollectionLayoutSection(group: group)

return section
}

尽管一切都配置正确,但按钮的高度约束会中断,“估计”会变成绝对值。

Button cell at an incorrect size

成功实现

static func mapSection() -> NSCollectionLayoutSection {
/*
Notice that I estimate my button cell to be 500 points high
It's way too much. But since it's an estimation and we're doing things well,
it doesn't really matter to the end result.
*/
let heightDimension = NSCollectionLayoutDimension.estimated(500)

let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: heightDimension)
let item = NSCollectionLayoutItem(layoutSize: itemSize)

let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: heightDimension)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

let section = NSCollectionLayoutSection(group: group)

return section
}

唯一真正的区别在于项目和组都设置为相同的估计高度尺寸。但它现在有效!

Button cell at the correct size

关于ios - 具有估计高度的 Collection View 组合布局不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58339188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com