【问题标题】:iOS 8 Today Widget fit height of UITableView using Auto LayoutiOS 8 Today Widget 使用自动布局来适应 UITableView 的高度
【发布时间】:2014-09-11 06:39:33
【问题描述】:

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

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

我不能做的是让小部件调整大小以包裹表格视图。目前我只剩下一张大小合适的桌子和很多空白空间。

【问题讨论】:

  • 您是如何实现以下目标的? 我可以在表格视图高度上使用带有出口约束的自动布局来调整表格大小以仅适合包含内容的行。

标签: uitableview autolayout ios8


【解决方案1】:

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

self.preferredContentSize = self.tableView.contentSize;

您应该在每次重新加载 tableView 的数据时设置此项,并且高度会发生变化。您不必担心 contentSize 的宽度,因为系统会忽略它。

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

self.view.translatesAutoresizingMaskIntoConstraints = NO;

这应该可以修复约束警告。

【讨论】:

    【解决方案2】:

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

    Interface Builder 中的常量

    只需将表格视图粘贴到视图控制器的视图边缘即可。

    代码中的常量

    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.
    

    【讨论】:

    • 我会等待将此标记为正确答案,看看是否有人可以提出更好的建议。
    • 如果我的上述答案对您有用,您能否将其标记为正确?谢谢。
    • 嘿@HenryTKirk,是的,您的解决方案比我发现的要好。你应该得到正确的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2015-05-21
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多