ios - UITableView header 上的自动布局

标签 ios autolayout

我一直在寻找关于如何将自动布局添加到 UITableView 的明确答案。到目前为止,我的代码如下所示:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UINib *nib = [UINib nibWithNibName:@"HomeHeaderView" bundle:nil];
    UIView *headerView = (UIView *)[nib instantiateWithOwner:self options:nil][0];
    [headerView.layer setCornerRadius:6.0];
    [headerView setTranslatesAutoresizingMaskIntoConstraints:NO];

//    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(headerView);
//    NSMutableArray *headerConstraints = [[NSMutableArray alloc] init];
//    [headerConstraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[headerView]-|" options:0 metrics:nil views:viewsDictionary]];
//        [headerConstraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[headerView]-|" options:0 metrics:nil views:viewsDictionary]];
//    [self.actionsTableView addConstraints:headerConstraints];
//    [self.view addSubview:headerView];
    tableView.tableHeaderView = headerView;
    [headerView layoutSubviews];

    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:300];
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:90];
    [self.view addConstraints:@[centerX, centerY, width, height]];

    return headerView;
}

我的标题 View 基本上有一个 nib 文件,我想将该 nib 置于我的 UITableViewHeader 的中心。我希望它在纵向和横向方向上相应地增长和缩小。老实说,我不确定我是否正确设置了约束。我不确定我的 toItem 应该是 View Controller 的 View ,还是 tableview 本身。

我也不知道我是否应该将标题 View 作为 subview 添加到 View Controller 的 View 或 TableView 本身。

或者,我不确定设置 tableView.tableHeaderView = headerView 是否足够。

我真的不知道对于这样的事情最好的做法是什么。我不确定这一切是否也可以在 IB 中完成。目前,使用您看到的代码,我得到了这个错误:

'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'

因为那个错误,我添加了[headerView layoutSubviews]

对此有何想法?提前致谢!

最佳答案

真正的问题是您混淆了 viewForHeaderInSection: 和表格的 headerView。它们是无关的。

前者是部分 的标题。您从委托(delegate)方法返回 View 。

后者是 的标题。您设置 View ,可能在您的 viewDidLoad 中。

约束以正常方式运行。但它们应该只是对其 subview 的内部约束。在您形成它时, View 不在您的界面中。而它的大小和位置,那时候还不是你说了算。如果它是部分标题,它将自动调整大小以正确适应(根据表格的宽度和表格或代表对标题高度的声明)。如果它是表格标题,您可以为其指定一个绝对高度,但它的宽度将被调整以正确适应。

这是一个完整的示例,它构建了一个对其 subview 具有内部约束的节标题。

- (UIView *)tableView:(UITableView *)tableView 
        viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView* h =
        [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Header"];
    if (![h.tintColor isEqual: [UIColor redColor]]) {
        h.tintColor = [UIColor redColor];
        h.backgroundView = [UIView new];
        h.backgroundView.backgroundColor = [UIColor blackColor];
        UILabel* lab = [UILabel new];
        lab.tag = 1;
        lab.font = [UIFont fontWithName:@"Georgia-Bold" size:22];
        lab.textColor = [UIColor greenColor];
        lab.backgroundColor = [UIColor clearColor];
        [h.contentView addSubview:lab];
        UIImageView* v = [UIImageView new];
        v.tag = 2;
        v.backgroundColor = [UIColor blackColor];
        v.image = [UIImage imageNamed:@"us_flag_small.gif"];
        [h.contentView addSubview:v];
        lab.translatesAutoresizingMaskIntoConstraints = NO;
        v.translatesAutoresizingMaskIntoConstraints = NO;
        [h.contentView addConstraints:
         [NSLayoutConstraint 
          constraintsWithVisualFormat:@"H:|-5-[lab(25)]-10-[v(40)]"
          options:0 metrics:nil views:@{@"v":v, @"lab":lab}]];
        [h.contentView addConstraints:
         [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|[v]|"
           options:0 metrics:nil views:@{@"v":v}]];
        [h.contentView addConstraints:
         [NSLayoutConstraint
          constraintsWithVisualFormat:@"V:|[lab]|"
           options:0 metrics:nil views:@{@"lab":lab}]];
    }
    UILabel* lab = (UILabel*)[h.contentView viewWithTag:1];
    lab.text = self.sectionNames[section];
    return h;
}

关于ios - UITableView header 上的自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18858810/

相关文章:

ios - 过期的 iO​​S 开发者/分发证书

ios - 检查是否已应用 NSLayoutConstraint

xcode - iOS 7 - 调整状态栏

ios - 调整 UICollectionView 的大小以使其内容适合 AutoLayout View

ios - 自动布局中的 "Use current canvas value"是什么意思?

ios - 简单的 iOS 委托(delegate)协议(protocol)不适用于在 View Controller 之间传递字符串

ios - CollectionView 委托(delegate)在 URL 连接完成之前调用

c++ - 无法使用 Xcode 9 运行项目,许多新的 C++ 语义问题

ios - 即使用户重新安装后也限制 iOS 应用程序

ios - Autolayout UIImageView 具有不遵循约束的编程调整大小