iOS 8 Today小部件使用自动布局匹配UITableView的高度

iOS 8 Today小部件使用自动布局匹配UITableView的高度,uitableview,autolayout,ios8,Uitableview,Autolayout,Ios8,我正在使用Today扩展,显示一个UITableView。只有几个项目,但我也得到了空行 我可以在表格视图高度上使用带有出口约束的自动布局来调整表格大小,以便只适合包含内容的行 我不能做的是调整小部件的大小以包装表视图。目前,我只剩下一个大小合适的表和大量的空白空间。我找到了一个解决方案,它可以根据需要编译和渲染,但会触发一个警告 界面生成器中的常量 只需将表视图粘贴到视图控制器的视图边缘即可 代码中的常量 在视图将出现中,我们可以将视图控制器视图的高度设置为与表视图contentSize相同

我正在使用Today扩展,显示一个
UITableView
。只有几个项目,但我也得到了空行

我可以在表格视图高度上使用带有出口约束的自动布局来调整表格大小,以便只适合包含内容的行

我不能做的是调整小部件的大小以包装表视图。目前,我只剩下一个大小合适的表和大量的空白空间。

我找到了一个解决方案,它可以根据需要编译和渲染,但会触发一个警告

界面生成器中的常量 只需将表视图粘贴到视图控制器的视图边缘即可

代码中的常量 在
视图将出现
中,我们可以将视图控制器视图的高度设置为与表视图
contentSize
相同

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.view
                                                                    attribute:NSLayoutAttributeHeight
                                                                    relatedBy:0
                                                                       toItem:nil
                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                   multiplier:1
                                                                     constant:self.tableView.contentSize.height];
heightConstraint.priority = UILayoutPriorityRequired;

[self.view addConstraint:heightConstraint];
[self.view needsUpdateConstraints];
[self.view setNeedsLayout];
警告 上面的代码工作正常,还解决了必须在表视图高度上添加出口约束的问题,但会生成以下警告:

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:0x7af81290 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7af7f560(171)]>",
    "<NSLayoutConstraint:0x7e1c1010 V:[UIView:0x7af7f560(132)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7af81290 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7af7f560(171)]>

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.
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。

如果希望小部件的控制器为tableView的高度,请执行以下操作:

self.preferredContentSize = self.tableView.contentSize;
每次重新加载tableView的数据时,如果高度发生变化,则应设置此值。您不必担心contentSize的宽度,因为系统会忽略它

要更正上述答案,您需要添加:

self.view.translatesAutoresizingMaskIntoConstraints = NO;

这将修复约束警告。

我将等待将此标记为正确答案,看看是否有人能提出更好的建议。如果我的上述答案对您有效,您能将其标记为正确答案吗?谢谢。嘿@HenryTKirk,是的,你的解决方案比我发现的要好。你应该得到正确的答案!你是如何做到以下几点的?我可以在表格视图高度上使用带有出口约束的自动布局来调整表格大小,以便只适合包含内容的行。