【问题标题】:How to add space between header text and tableview如何在标题文本和表格视图之间添加空格
【发布时间】:2018-04-04 15:31:15
【问题描述】:

在 iOS 11 中,我注意到在控制中心他们有一个标题视图,它的底部和表格视图单元格之间有空间。您将如何实现这一目标? heightForHeaderInSection 仅更改标题顶部与其上方任何内容之间的高度,但不更改底部和 tableview 之间的空间。

更新: 这是对我有用的代码,其他任何人都想知道。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, tableView.frame.size.width - 30, 50)];
    headerLabel.text = @"Text goes here";
    headerLabel.numberOfLines = 0;
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.textAlignment = NSTextAlignmentLeft;
    [headerLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0]];
    // I could not figure out the exact text color :(
    [headerLabel setTextColor:[UIColor headingTextColor]];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, headerLabel.frame.size.width, 200)];
    [headerView addSubview:headerLabel];

    return headerView;
}

【问题讨论】:

标签: ios objective-c xcode ios11


【解决方案1】:

我会尝试通过实现tableView(_:viewForHeaderInSection:) 并提供将用作标题的自定义视图来做到这一点。您可以在其中添加一个标签作为子视图并根据需要放置它。

tableView(_:viewForHeaderInSection:) 可与 UITableViewAutomaticDimension 配合使用,但如果您预先知道标头高度,则可以简单地将其与 heightForHeaderInSection 结合使用。

【讨论】: