自动布局:什么创建名为UIView-Encapsulated-Layout-Width&Height的约束?

Reu*_*ton 68 uitableview ios autolayout uicollectionview

我的布局约束在Interface Builder中很好,但是由于框架的某些部分应用了我真正不想要的固定高度和宽度约束,因此在运行时会发生异常.为什么他们在那里,以及如何关闭他们?

它们是记录列表中显示的最后两个约束:

2014-04-26 09:02:58.687 BBCNews[32058:60b] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0xbf478a0 UIView:0xbf4a3c0.height == 0.28125*UIView:0xbf4a3c0.width>",
    "<NSLayoutConstraint:0xbf47190 UIView:0xbf4a3c0.leading == BNMyNewsCell_landscape:0xbf48b10.leading>",
    "<NSLayoutConstraint:0xbf47160 UIView:0xbf4a3c0.trailing == BNMyNewsCell_landscape:0xbf48b10.trailing>",
    "<NSLayoutConstraint:0xbf47130 BNMyNewsCell_landscape:0xbf48b10.bottom == UIView:0xbf4a3c0.bottom>",
    "<NSLayoutConstraint:0xbf47100 UIView:0xbf4a3c0.top == BNMyNewsCell_landscape:0xbf48b10.top>",
    "<NSLayoutConstraint:0xd4c3c40 'UIView-Encapsulated-Layout-Width' H:[BNMyNewsCell_landscape:0xbf48b10(304)]>",
    "<NSLayoutConstraint:0xd4c38a0 'UIView-Encapsulated-Layout-Height' V:[BNMyNewsCell_landscape:0xbf48b10(290)]>"
}
Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0xbf478a0 UIView:0xbf4a3c0.height == 0.28125*UIView:0xbf4a3c0.width>
Run Code Online (Sandbox Code Playgroud)

Reu*_*ton 89

基于一吨的观察,我相信(但不能确切知道)的约束命名UIView-Encapsulated-Layout-Width,并UIView-Encapsulated-Layout-Height通过创建UICollectionView和朋友,存在以执行返回的大小sizeForItemAtIndexPath委托方法.我想这是为了确保UICollectionViewCell设置cellForItemAtIndexPath最终达到它被告知的大小.

这回答了我最初的问题.第二个问题是为什么约束不可满足?细胞的内在高度应该是相同的UIView-Encapsulated-Layout-Height.同样,我不确定,但我怀疑这是一个舍入错误(即内部高度达到200.1像素,UIView-Encapsulated-Layout-Height可能四舍五入到200.我想出的修复只是降低相关单元约束的优先级允许UIView-Encapsulated-Layout-Height有最后一个字.

  • 我完全没有希望在控制台中修复约束警告,但不知怎的,我遇到了另一个约束问题,引导我到这个页面,我很高兴在这里找到你的答案.我只是将优先级降低到999并且繁荣,所有我的约束警告都消失了:)非常感谢你:) :) :) (10认同)
  • 这修复了我的约束破坏和相应的警告消息,但现在我的文本视图已折叠,直到我将单元格滚出视图然后重新进入. (2认同)

Yer*_*erk 42

这可能无法回答你的问题,但它可以帮助像我这样从搜索到这里的其他人.

我得到一个奇怪的AutoLayout破坏约束错误伴随着一个UIView-Encapsulated-Layout-Width约束,因为我正在添加一个tableHeaderView尚未使用AutoLayout调整大小的表视图.所以系统试图在一个带有框架的tableview中应用我的头部子视图的约束{0,0,0,0}.由于UITableView喜欢控制其元素的宽度,因此将其生成的宽度约束UIView-Encapsulated-Layout-Width设置为零,从而导致与我期望320 + pt宽度的标题元素的各种混淆.

摘要:确保在通过AutoLayout调整tableview之后添加/操作补充/页眉/页脚视图.

  • 我想确定自己做的正确。您能解释一下如何确定这些操作在自动版面设计之后发生吗? (2认同)
  • @nwales当iOS尝试在0 x视图中布局约束时,会出现问题.要避免此错误,只需确保容器视图(在我的情况下,tableHeaderView)布局或手动调整大小.您可以通过在设置约束之前初始化具有近似大小的容器视图来执行此操作,或者如果您希望纯Autolayout布局容器视图的约束,则在设置子视图约束之前调用`[thatContainer layoutIfNeeded]`.如果您没有收到错误并且您的观点是正确的,那么您无需担心. (2认同)

Zol*_*tán 6

我面对同样奇怪的约束,不知道为什么,直到我记得这个愚蠢的translatesAutoresizingMaskIntoConstraints财产.设置此项以false解决问题.后台发生的事情是自动调整大小掩码(iOS的旧布局引擎)将转换为约束.通常,您不需要这些约束并且想要自己的约束.在这种情况下,你应该将此属性设置为false,你会没事的:

view.translatesAutoresizingMaskIntoConstraints = false
Run Code Online (Sandbox Code Playgroud)

  • 你到底是在哪个“视图”上设置的?是 UITableViewCell 吗? (2认同)

Rei*_*ill 6

敲了我的头一段时间后,我发现了这个链接。就我而言,当我从 UIVieController 使用 insertRows 或 deleteRows 时,它发生在 UITableViewHeaderFooterView 上。设置了“estimatedSectionHeaderHeight”和“estimatedRowHeight”,我的约束重做了 3 次...显示的错误是:

\n
"<NSLayoutConstraint:0x280648140 H:|-(8)-[UIImageView:0x106e94860]   (active, names: \'|\':DAT_Air_Vinyl.ExportsAlbumTableViewHeader:0x106e8fb50 )>",\n"<NSLayoutConstraint:0x280648230 H:[UIImageView:0x106e94860]-(8)-[DAT_Air_Vinyl.MainLabel:0x106ea5750\'The Coral - The Invisible...\']   (active)>",\n"<NSLayoutConstraint:0x280648410 H:[UIButton:0x106ea5a40]-(8)-|   (active, names: \'|\':DAT_Air_Vinyl.ExportsAlbumTableViewHeader:0x106e8fb50 )>",\n"<NSLayoutConstraint:0x280648460 H:[DAT_Air_Vinyl.MainLabel:0x106ea5750\'The Coral - The Invisible...\']-(8)-[UIButton:0x106ea5a40]   (active)>",\n"<NSLayoutConstraint:0x2806493b0 \'UIView-Encapsulated-Layout-Width\' DAT_Air_Vinyl.ExportsAlbumTableViewHeader:0x106e8fb50.width == 0   (active)>"\n
Run Code Online (Sandbox Code Playgroud)\n

正如链接中所述:

\n

"当您使用某些动画类型执行 insertRows 或 deleteRows 操作时,UIKit 会将行高从 0 动画化到全高或返回。在该动画的 0 端,如果将整个垂直轴设置为优先级,则无法求解布局方程=1000。但是将一个约束降低到 999 \xe2\x80\x93 表示底部空间到超级视图边距 \xe2\x80\x93 ,一切都很好;内容将只是下拉,在单元格 \xe2\x80\ 之外x99s 界限。 ”。

\n

解决方案是将 UIImageView 的主导优先级设置为 999(或更低至 1000)。

\n