【问题标题】:Using auto layout and systemLayoutSizeFittingSize使用自动布局和 systemLayoutSizeFittingSize
【发布时间】: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


【解决方案1】:

我发现了问题,但我不明白为什么。对于 UITableViewHeader,我无法为视图执行典型的自动布局。也就是说,我必须将translatesAutoresizingMaskIntoConstraints 设置为true。实际上,我不能对标题视图使用自动布局,我必须手动设置框架。

在标题视图中,我可以成功地为子视图使用自动布局。

所以我的算法是这样构造tableHeaderView的:

  • 使用任意帧创建标题视图
  • 计算约束(标题视图的子视图)
  • 设置标题视图的frame.width 以匹配父视图tableView.frame.width
  • 布局标题视图
  • 使用systemLayoutSizeFittingSize 计算高度
  • 更新标题视图的frame.height

这对我有用。如果我创建标题视图并将translatesAutoresizingMaskIntoConstraints 设置为false,我无法让它工作。

【讨论】:

    【解决方案2】:

    根据Apple Visual Format Language,以下内容似乎在您的 HeaderView 上设置了“0”高度约束。

    例如

    "<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
    

    我将首先对此进行调查,因为高度为 0 可能会弄乱您对顶部和底部间距的其他标签约束。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2013-08-16
      相关资源
      最近更新 更多