【发布时间】:2021-04-26 07:59:34
【问题描述】:
目标是为包含固定纵横比UIImageView(加上下面的一些其他视图,包括可变高度UILabels 和UIStackViews)的全宽单元格配置UICollectionViewCompositionalLayout,并让单元格适当地自动调整大小。我的天真尝试不起作用,我不知道为什么。我在这里介绍的简单案例在运行时给了我一堆约束错误。
我抄袭了this answer 的代码,但看起来很简单:
override func viewDidLoad() {
super.viewDidLoad()
let size = NSCollectionLayoutSize(
widthDimension: NSCollectionLayoutDimension.fractionalWidth(1),
heightDimension: NSCollectionLayoutDimension.estimated(100)
)
let item = NSCollectionLayoutItem(layoutSize: size)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitem: item, count: 1)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 10
let layout = UICollectionViewCompositionalLayout(section: section)
collectionView.collectionViewLayout = layout
}
一旦我滚动,我就会得到这些约束错误:
2021-01-21 20:29:18.311491+0100 CompositionalLayoutDynamicHeight[96399:2597528] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x6000020a82d0 UIImageView:0x138818600.width == 1.33333*UIImageView:0x138818600.height (active)>",
"<NSLayoutConstraint:0x6000020a8460 V:|-(0)-[UIImageView:0x138818600] (active, names: '|':UIView:0x13881c4d0 )>",
"<NSLayoutConstraint:0x6000020a84b0 H:[UIImageView:0x138818600]-(0)-| (active, names: '|':UIView:0x13881c4d0 )>",
"<NSLayoutConstraint:0x6000020a8500 V:[UIImageView:0x138818600]-(0)-| (active, names: '|':UIView:0x13881c4d0 )>",
"<NSLayoutConstraint:0x6000020a8550 H:|-(0)-[UIImageView:0x138818600] (active, names: '|':UIView:0x13881c4d0 )>",
"<NSLayoutConstraint:0x6000020a85a0 'UIIBSystemGenerated' CompositionalLayoutDynamicHeight.CollectionViewCell:0x13881c280.leading == UIView:0x13881c4d0.leading (active)>",
"<NSLayoutConstraint:0x6000020a85f0 'UIIBSystemGenerated' H:[UIView:0x13881c4d0]-(0)-| (active, names: '|':CompositionalLayoutDynamicHeight.CollectionViewCell:0x13881c280 )>",
"<NSLayoutConstraint:0x6000020a8640 'UIIBSystemGenerated' CompositionalLayoutDynamicHeight.CollectionViewCell:0x13881c280.top == UIView:0x13881c4d0.top (active)>",
"<NSLayoutConstraint:0x6000020a8690 'UIIBSystemGenerated' V:[UIView:0x13881c4d0]-(0)-| (active, names: '|':CompositionalLayoutDynamicHeight.CollectionViewCell:0x13881c280 )>",
"<NSLayoutConstraint:0x6000020a60d0 'UIView-Encapsulated-Layout-Height' CompositionalLayoutDynamicHeight.CollectionViewCell:0x13881c280.height == 292.667 (active)>",
"<NSLayoutConstraint:0x6000020a6120 'UIView-Encapsulated-Layout-Width' CompositionalLayoutDynamicHeight.CollectionViewCell:0x13881c280.width == 390 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000020a82d0 UIImageView:0x138818600.width == 1.33333*UIImageView:0x138818600.height (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
似乎 AutoLayout 正在创建与纵横比约束冲突的 UIView-Encapsulated-Layout-Height 约束,但为什么不只是调整大小以适应它呢?
【问题讨论】:
-
我们刚刚遇到了另一个非常相似的情况,并且再次修复了将
NSCollectionLayoutSize(... heightDimension:)中的高度估计值更改为over 估计值。如果低估,我们会在运行时违反约束。如果是高估,一切都很好。 -
这里有同样的问题,但在水平轴上。高估无济于事。它不断打破我的限制......我希望能够将自动大小的视图与固定大小的视图混合在一起。
标签: ios uicollectionview autolayout interface-builder uicollectionviewlayout