【问题标题】:UIView-Encapsulated-Layout-Height: Invalid constraints in tableHeaderViewUIView-Encapsulated-Layout-Height:tableHeaderView 中的约束无效
【发布时间】:2014-10-30 22:13:00
【问题描述】:

我正在尝试找到最简洁的方法来调整我的 UITableView 的 tableHeaderView 的高度,它本身只是一个带有多行标签的视图。似乎自动布局应该能够很容易地计算出高度。

我这样创建标签:

DDInsetLabelView *header = [[DDInsetLabelView alloc] initWithFrame:CGRectZero];
header.insets = UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f);
header.label.text = [NSString stringWithFormat:@"%@\n%@", self.categoryTitle, self.subcategoryTitle];
header.label.numberOfLines = 0;
header.label.textAlignment = NSTextAlignmentCenter;
self.tableView.tableHeaderView = header;

在标签类中,我这样处理布局:

- (void)updateConstraints {
    [super updateConstraints];

    NSDictionary *metrics = @{@"t": @(self.insets.top), @"l": @(self.insets.left),
                              @"b": @(self.insets.bottom), @"r": @(self.insets.right)};
    NSDictionary *views = NSDictionaryOfVariableBindings(_label);
    self.label.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(t)-[_label]-(b)-|" options:0 metrics:metrics views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(l)-[_label]-(r)-|" options:0 metrics:metrics views:views]];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.label.preferredMaxLayoutWidth = self.label.frame.size.width;
    [super layoutSubviews];
}

现在,这是我在控制器中计算标题视图高度的方法:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    UIView *header = self.tableView.tableHeaderView;
    NSLayoutConstraint *headerWidthConstraint = [NSLayoutConstraint
                                                 constraintWithItem:header attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
                                                 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:header.frame.size.width];
    [header addConstraint:headerWidthConstraint];
    CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    [header removeConstraint:headerWidthConstraint];

    header.frame = CGRectMake(0, 0, header.frame.size.width, height);
    header.translatesAutoresizingMaskIntoConstraints = YES;
    self.tableView.tableHeaderView = header;
}

看起来这在视觉上工作正常,但我在控制台中不断收到以下错误:

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:0x7fb2e5bb7540 'UIView-Encapsulated-Layout-Height' V:[DDInsetLabelView:0x7fb2e5b53250(0)]>",
    "<NSLayoutConstraint:0x7fb2e5bb84b0 V:|-(10)-[UILabel:0x7fb2e5b65200'Custom Box\nCustomize with...']   (Names: '|':DDInsetLabelView:0x7fb2e5b53250 )>",
    "<NSLayoutConstraint:0x7fb2e5bb8500 V:[UILabel:0x7fb2e5b65200'Custom Box\nCustomize with...']-(10)-|   (Names: '|':DDInsetLabelView:0x7fb2e5b53250 )>"
)

我假设UIView-Encapsulated-Layout-Height 代表一些内部计算的标题高度值。使用自动布局计算标题高度时如何避免此警告??

【问题讨论】:

  • 有同样的问题,但使用更简单的方法。使用 Xcode 6.3.2,我可以将我的自动布局、基于 xib 的视图分配给 tableHeaderView 属性。高度和宽度只是工作。但是,当我切换到另一个视图,然后返回时,我会收到相同的 NSLayoutConstraint 警告,并且tableHeaderView 会无缘无故地更改高度...尤其是 0。:(

标签: ios uitableview autolayout


【解决方案1】:

尝试删除该行...header.translatesAutoresizingMaskIntoConstraints = YES;

另外,在创建标题时添加[header setTranslatesAutoresizingMaskIntoConstraints:NO]

【讨论】:

    猜你喜欢
    • 2015-10-09
    • 2014-02-05
    • 2021-01-14
    • 2021-05-21
    • 1970-01-01
    • 2014-06-12
    • 2015-04-09
    • 2016-03-04
    • 2015-06-16
    相关资源
    最近更新 更多