【问题标题】:AutoLayout exceptions iOS6iOS6 自动布局异常
【发布时间】:2014-01-22 07:52:43
【问题描述】:

我目前正在使用带有自动布局的 xib 在集合视图中创建自定义单元格。我有两个方向的不同单元格大小。因此,我根据方向更改单元格的宽度和高度。

问题是,当我更改设备方向时,我会在 iOS6 中收到大量警告/异常。

但是,当我在 iOS7 中执行相同操作时,没有警告。

虽然它不会影响我的功能,但我想删除所有这些警告。

这是我的XIB file

以下是我得到的错误/警告/异常。

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:0x164371f0 V:[UILabel:0x16437240(21)]>",
"<NSLayoutConstraint:0x16438050 V:[UILabel:0x16437240]-(1)-[UILabel:0x16437670]>",
"<NSLayoutConstraint:0x16437fd0 V:[UILabel:0x16437670]-(5)-|   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSLayoutConstraint:0x16437f50 V:[UILabel:0x16436e10]-(0)-[UILabel:0x16437240]>",
"<NSLayoutConstraint:0x16437e90 V:|-(7)-[UILabel:0x16436e10]   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16354d00 h=--& v=--& V:[UICollectionView:0xb24f200(264)]>",
"<NSAutoresizingMaskLayoutConstraint:0x163513b0 h=-&- v=-&- LibraryCollectionViewCell:0x16436ae0.height == UICollectionView:0xb24f200.height - 264>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x164371f0 V:[UILabel:0x16437240(21)]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我尝试过[cell setTranslatesAutoresizingMaskIntoConstraints:NO],但它反而使应用程序崩溃。

谁能提供一些解决方案?

【问题讨论】:

    标签: ios objective-c ipad autolayout


    【解决方案1】:
    "<NSLayoutConstraint:0x164371f0 V:[UILabel:0x16437240(21)]>",
    "<NSLayoutConstraint:0x16438050 V:[UILabel:0x16437240]-(1)-[UILabel:0x16437670]>",
    "<NSLayoutConstraint:0x16437fd0 V:[UILabel:0x16437670]-(5)-|   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
    "<NSLayoutConstraint:0x16437f50 V:[UILabel:0x16436e10]-(0)-[UILabel:0x16437240]>",
    "<NSLayoutConstraint:0x16437e90 V:|-(7)-[UILabel:0x16436e10]   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x16354d00 h=--& v=--& V:[UICollectionView:0xb24f200(264)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x163513b0 h=-&- v=-&- LibraryCollectionViewCell:0x16436ae0.height == UICollectionView:0xb24f200.height - 264>"
    

    好的,根据您的限制...

    (括号中的数字是参考的最后3位数字)。

    Label (240) with height constraint fixed to 21.
    Label (240) with a vertical gap of 1 to label (670).
    Label (670) with a vertical gap of 5 to the superview (ae0) bottom.
    Label (e10) with a vertical gap of 7 to the superview (ae0) top.
    
    UICollectionView (200) with fixed height of 264.
    LibraryCollectionViewCell (ae0) with an equal height to the collection view - 264 (200).
    

    好的!有你的问题!亚当萨维奇>

    列表中的最后一个约束将使单元格的高度为 0。(264 - 264 = 0) 但是,在单元格中,您有一个固定高度为 21 和固定垂直间隙为 7、5 和 1 的标签。

    264 - 264 = 0
    21 + 7 + 5 + 1 = 36
    
    36 != 0
    

    您可以通过多种方式解决此问题。移除单元格和集合视图之间的约束。移除标签上的固定高度约束。等等……

    我建议您画出您希望它如何工作(在纸上)并写下您需要的限制条件。或者将您正在尝试做的事情的图片放入您的问题中,以便我们尝试提供帮助。

    【讨论】:

    • 你似乎是对的。但是,当我尝试删除垂直约束时,视图会扭曲。此外,在将其从横向旋转回纵向时,我收到 H: 的警告
    • 完全删除了自动布局并修复了自动调整大小蒙版的问题。无论如何都接受你的回答,因为它确实在我尝试时删除了警告。
    【解决方案2】:

    Auto layoutiOS 6.0iOS 7.0 的方式发生了很大变化。

    这在通过X-Code 4.xX-Code 5.x 实现的自动布局中非常明显。

    由于 Apple 现在希望应用程序可以在 X-Code 5.x 中构建,我建议使用相同的版本。

    同时检查base SDK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      相关资源
      最近更新 更多