【发布时间】:2015-02-05 04:31:37
【问题描述】:
我有一个非常简单的观点,我无法终生解决问题。我已经成功地使用了自动布局和systemLayoutSizeFittingSize 来实现相当复杂的自定义UITableViewCells。这是我第一次将它用于UIView(用于UITableView.tableHeaderView),但我无法让它工作。
我想要覆盖整个视图的视图中有一个简单的标签。我已将Trailing、Leading、Top 和Bottom 约束添加到针对父级相应边缘的标签。当我这样做时,systemLayoutSizeFittingSize 计算出正确的height,但我收到以下错误:
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:0x7fbb71d9a310 V:|-(5)-[UILabel:0x7fbb71df5120'Test Label'] (Names: '|':mailapp.MailTableHeaderView:0x7fbb71d2c490 )>",
"<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>",
"<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
现在,如果我删除标签上的“底部”约束,自动布局可以正常工作,但现在 systemLayoutSizeFittingSize 返回 0。
什么是使约束满足自动布局引擎同时让systemLayoutSizeFittingSize 计算正确大小的正确方法?
正如我所提到的,最令人困惑的是我已经成功地将它用于复杂的自动布局,但我注意到所有这些都是UITableViewCells。我不确定是否有区别。
【问题讨论】:
-
你在使用 UILayoutFittingExpandedSize 吗?
-
@Fonix 不,我正在使用 UILayoutFittingCompressedSize
-
尝试为标签添加高度约束,设置为您希望标签的最小高度,然后使高度约束大于或等于
-
尝试在初始化器中添加
yourHeaderView.contentView.setTranslatesAutoresizingMaskIntoConstraints(false)。 -
尝试添加相对于 self.view 的底部和右侧约束,同时显示添加约束的代码
标签: ios objective-c swift autolayout