我正在使用Today Extension,显示UITableView
。只有几个项目,但我也得到空行。
我可以使用自动布局在表格视图高度上使用出口约束来调整表格大小以仅适合包含内容的行。
我无法做的是让小部件调整大小以包装表视图。目前我留下了一张大小合适的桌子和很多空地。
答案 0 :(得分:15)
如果您希望窗口小部件的控制器成为tableView的高度,请执行以下操作:
self.preferredContentSize = self.tableView.contentSize;
每次重新加载tableView的数据时都应设置此值,高度会发生变化。您不必担心contentSize的宽度,因为系统会忽略它。
要纠正上述答案,您需要添加:
self.view.translatesAutoresizingMaskIntoConstraints = NO;
那应该修复约束警告。
答案 1 :(得分:0)
我找到了解决方案,它会根据需要进行编译和渲染,但会触发警告。
只需将表格视图固定在视图控制器的视图边缘。
在viewWillAppear
中,我们可以将视图控制器视图的高度设置为与表格视图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.