【发布时间】:2021-05-24 04:18:29
【问题描述】:
刚刚迈出 IOS 开发的第一步,我想将我的 Android 应用程序带到 IOS。
UICollectionView 的问题。我需要单元格具有固定的恒定宽度,并且单元格内的图像 - 具有固定的高度。因此,我将 StackView 的宽度限制设置为 105,将 ImageView 的高度限制设置为 147。
一切看起来都如预期的那样,但是我在控制台中收到警告:
2021-02-21 22:23:17.490472+0200 myapp-ios[20727:783655] [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:0x600002193890 UIStackView:0x7f90efc173d0.width == 105 (active)>",
"<NSLayoutConstraint:0x6000021938e0 H:[UIStackView:0x7f90efc173d0]-(0)-| (active, names: '|':UIView:0x7f90efc1d840 )>",
"<NSLayoutConstraint:0x600002193930 H:|-(0)-[UIStackView:0x7f90efc173d0] (active, names: '|':UIView:0x7f90efc1d840 )>",
"<NSLayoutConstraint:0x600002193a20 'UIIBSystemGenerated' myapp_ios.ShowCell:0x7f90efc23ec0.leading == UIView:0x7f90efc1d840.leading (active)>",
"<NSLayoutConstraint:0x600002193a70 'UIIBSystemGenerated' H:[UIView:0x7f90efc1d840]-(0)-| (active, names: '|':myapp_ios.ShowCell:0x7f90efc23ec0 )>",
"<NSLayoutConstraint:0x6000021967b0 'UIView-Encapsulated-Layout-Width' myapp_ios.ShowCell:0x7f90efc23ec0.width == 50 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002193890 UIStackView:0x7f90efc173d0.width == 105 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
我还尝试将 ImageView 的宽度和高度限制设置为 105x147,它看起来一样,并且警告几乎相同。基本上,如果没有设置宽度或高度约束 - 没有警告,一旦我添加它们中的任何一个 - 它就会出现。
以下是限制条件:
感谢您帮助我了解潜在问题。
【问题讨论】:
-
使用wtfautolayout.com。如果您要约束前缘和后缘,则不需要堆栈视图宽度约束。将推断宽度。要固定单元格的大小,请使用集合视图流布局委托函数。
-
委托函数返回 CGSize,其中所有字段必须 > 0。所以我知道所需的宽度,但高度 - 不,我只知道图像视图的所需高度,而不知道整个单元格的高度.
-
你需要你的单元格自动调整大小stackoverflow.com/questions/25895311/…
标签: ios xcode uicollectionview constraints fixed-width