自动布局:创建名为 UIView-Encapsulated-Layout-Width & 的约束的原因高度? [英] Auto-layout: What creates constraints named UIView-Encapsulated-Layout-Width & Height?

查看:17
本文介绍了自动布局:创建名为 UIView-Encapsulated-Layout-Width & 的约束的原因高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My layout constraints are fine in Interface Builder but an exception occurs at runtime thanks to some part of the framework applying fixed height and width constraints that I really don't want. Why are they there, and how to turn them off?

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

They're the last two constraints shown in the logged list:

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>

推荐答案

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

Based on a ton of observation I believe (but cannot know for certain) that the constraints named UIView-Encapsulated-Layout-Width and UIView-Encapsulated-Layout-Height are created by UICollectionView and friends, and exist to enforce the size returned by the sizeForItemAtIndexPath delegate method. I guess it's there to ensure that the UICollectionViewCell set up by cellForItemAtIndexPath ends up the size that it was told it would be.

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

Which answers my initial question here. A second question is why were the constraints unsatisfiable? The cell's intrinsic height should have been the same as UIView-Encapsulated-Layout-Height. Again, I don't know for certain, but I suspect it was a rounding error (i.e. intrinsic height came to 200.1 pixels, the UIView-Encapsulated-Layout-Height maybe rounded to 200. The fix I came up with was to just lower the priority of the relevant cell constraint to allow UIView-Encapsulated-Layout-Height to have the last word.

这篇关于自动布局:创建名为 UIView-Encapsulated-Layout-Width &amp; 的约束的原因高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆