【发布时间】:2020-02-08 20:37:47
【问题描述】:
我希望我的应用针对包括文本大小在内的每个辅助功能选项进行优化。
我根据具有组合布局的部分制作了一个 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
当我在可访问性设置上设置更高的文本大小时,会发生以下情况:
单元格应该包含 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)
])
提前感谢您的帮助。
【问题讨论】:
-
您可能需要收听通知
UIContentSizeCategory.didChangeNotification刷新页面。 -
感谢您的评论。不幸的是,我不愿意在字体更改时收到通知,我试图让 UICollectionViewLayout 足够灵活,不必担心可访问性设置可以正常工作和显示。
-
当你的约束是完美的,改变
label.text可以自动刷新约束,但是目前改变系统字体并不会自动刷新约束,抱歉我没有找到支持这个论点的来源。 -
问题不是来自标签的约束,而是我给单元格的
estimated高度。如果我错了,请告诉我,但我希望在我的 NSCollectionLayoutDimension 对象中设置为高度的估计值与 UICollectionViewFlowLayout.automaticSize 一样。就像你在这里做的那样:flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
标签: ios swift uicollectionview ios13 ios-autolayout